Reputation: 86747
I'm doing a private Timestamp timestamp = new Timestamp(System.nanoTime());
in one of my entities when the object is created.
If I system.out the timestamp, I'm getting values like;
2282-08-24 11:25:00.506
2286-04-16 01:47:35.882
What is wrong here?
System.currentTimeMillis()
gives the correct date, but I need more accuracy.
What could be wrong here??
Upvotes: 4
Views: 2222
Reputation: 1055
Timestamp expects new Timestamp(miliseconds). If you give it nanoseconds (new Timestamp(nanoseconds) ---> 1000 times greater) you obtain a timestamp 1000 times in the future, whatever that means. This is the reason because the dates obtained are in the year 2282
Upvotes: 0
Reputation: 108889
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.
Upvotes: 1
Reputation: 340733
System.nanoTime()
is a completely abstract way of measuring time, it has something to do with the number of CPU cycles since the computer was started. It's completely unrelated to java.util.Date
that uses epoch time (number of milliseconds since 1970).
You are free to compare two different values of System.nanoTime()
to get very accurate time measurements (theoretically up to 1 nanosecond), but the absolute value taken alone is useless.
Upvotes: 10