4

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??

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 2
    Nothing, it just behaves as specified: http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html#Timestamp(long) – home Nov 02 '12 at 13:24
  • 4
    Have you bothered reading the documentation for System.currentTimeMillis(), System.nanoTime() and the Timestamp constructor? That should have answered your question in less time it took you to write this question. – jarnbjo Nov 02 '12 at 13:25
  • See [this](http://stackoverflow.com/a/11599287/1504523) answer to get a brief overview about `System.currentTimeMillis()` and `System.nanoTime()`. – Arno Nov 02 '12 at 19:06

3 Answers3

10

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I was not aware of this! Anyway is there an alternative to create the date with accuracy better than millis? – membersound Nov 02 '12 at 13:27
  • @membersound: [JavaDoc on `Date`](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html) (emphasis mine): "*The class `Date` represents a specific instant in time, with **millisecond precision**.*" | downvoter, care to explain? – Tomasz Nurkiewicz Nov 02 '12 at 13:35
  • @TomaszNurkiewicz Yes, you can create a higher precision than millis. It is not straightforward, but the gist of it, is recording the System.nanoTime at boot, then calculate the cost the of the actual System.nanoTime operation. You would then record a number of system.currenttimeinmillis as well as system.nanoTime and calibrate them using the average diff. And then, whenever you want the actual time in nanoseconds, you would do a System.nanoTime() and withdraw some calculations. I have it code. Maybe I can pull it out. – mjs Sep 20 '20 at 18:58
1

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.

documentation

McDowell
  • 107,573
  • 31
  • 204
  • 267
0

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

Chuidiang
  • 1,055
  • 6
  • 13