Answering your question:
what is the best way to get a timestamp in Java where you can always reset mileseconds?
For cases:
System.currentTimeMillis(), Calendar.getInstance(). getTimeInMillis(), new Date(). getTime(); and new Timestamp(). getTime();
You say you don’t want to remove in Rra, I’m not sure how you used it, but the simplest way I can imagine to do that is by dividing by 1000 and then multiplying by 1000. So you disregard 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));
}
}
Upshot:
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
I believe that the Mysql date time does not reach this level of accuracy. I know that in SQL Server there is the type
datetime2
with that level of accuracy– gmsantos
Out of curiosity: what application are you developing that needs this level of accuracy (milliseconds)?
– gmsantos
I may run out of a million seconds of timestamp, but as a developer it would be interesting to understand this behavior. Another issue is that in every way I tried to get the timestamp in Java it comes with milliseconds.
System.currentTimeMillis()
,Calendar.getInstance().getTimeInMillis()
,new Date().getTime();
andnew Timestamp().getTime();
I didn’t want to have to remove the milliseconds in the box.– noNihongo
Mysql version ? Fractional seconds is a new Feature in Mysql: http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-4.html
– gmsantos
The version of my Mysql is 5.1.62, which is the best way to get a timestamp in Java where I can always reset milesseconds, so part of my problem ends.
– noNihongo