Answering your question:
What is the best way to get a timestamp in Java where you can always zero the milliseconds?
For cases:
System.currentTimeMillis (), Calendar.getInstance (). getTimeInMillis (), new Date (). getTime (); and new Timestamp (). getTime ();
You say you do not want to remove it, I'm not sure how you used it, but the simplest way I imagine it is dividing by 1000 and then multiplying by 1000. So you ignore the last three digits of your date , which correspond to milliseconds. So:
new Timestamp((System.currentTimeMillis()/1000)*1000);
Example:
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
public class TesteTimestamp {
public static void main(String[] args) {
timestampComMili(System.currentTimeMillis());
timestampSemMili(System.currentTimeMillis());
timestampComMili(Calendar.getInstance().getTimeInMillis());
timestampSemMili(Calendar.getInstance().getTimeInMillis());
timestampComMili(new Date().getTime());
timestampSemMili(new Date().getTime());
//timestampComMili(new Timestamp().getTime());
//timestampSemMili(new Timestamp().getTime());
/* O código acima não é possível, pois não existe um
* construtor padrão a classe Timestamp. Veja em:
* http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
*/
}
public static void timestampComMili(long l) {
System.out.println("Com mili: " + new Timestamp(l));
}
public static void timestampSemMili(long l) {
System.out.println("Sem mili: " + new Timestamp((l/1000)*1000));
}
}
Result:
With mili: 2014-09-16 09: 48: 41,186
No mili: 2014-09-16 09: 48: 41.0
With mili: 2014-09-16 09: 48: 41,198
No mili: 2014-09-16 09: 48: 41.0
With mili: 2014-09-16 09: 48: 41,198
No mili: 2014-09-16 09: 48: 41.0