What is the best way to get the most accurate system time (hours minutes and seconds) correct upto milliseconds in java. I have been looking into Clock, Date and Calendar objects. I want to be able to timestamp a packet with the most accurate System time and send it over the network(accurate to Milli or even NanoSeconds if possible).
-
5The accuracy of the system clock depends on the system, not Java - the best you can hope for on most platforms is 15 milliseconds or so. `Instant.now()` will give you the current time. – Boris the Spider Jun 29 '16 at 17:53
-
@BoristheSpider I did look at Instant.now() aswell but it is giving me accuracy up only till three decimal places. If I use the calendar object and call the getTimeInMillis() on it it gives me a lot more accuracy. So I am confused. – Krish Jun 29 '16 at 17:56
-
Three decimal places of _what_? `Instant` [has nanos](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#getNano--). But as I said above, what units a particular timestamp is in is completely irrelevant, your OS is likely recording in 10's of milliseconds so whether your timestamp is in milli/micro/nano/pico seconds has no meaning. – Boris the Spider Jun 29 '16 at 17:58
-
@BoristheSpider This is what it reads. "2016-06-29T17:54:32.919Z" So thats 3 decimals off the seconds counter. Also this is UTC I am guessing I couldnt see a method to get localtime. Also Is there anyway to just snapshot this time so I can save it and perform operations on this time object? – Krish Jun 29 '16 at 18:11
-
1How does the default `String` representation of an `Instant` affect anything? You could also format it to only have a year. Please **please** PLEASE read at least one tutorial before posting further. – Boris the Spider Jun 29 '16 at 18:21
2 Answers
Java 9
The java.time framework supports values with up to nanosecond resolution. That means up to nine (9) digits of a decimal fraction of second.
In Java 9 and later, a new implementation of Clock
can capture the current moment up to that nanosecond resolution. But “your mileage may vary”. The actual resolution, and the actual accuracy, depend on the host hardware clock capability.
Instant instant = Instant.now();
2016-01-02T12:34:56.123456789Z
Java 8
Same as for Java 9, java.time classes support up to nanosecond resolution.
But the current moment is captured only up to millisecond resolution. That means only three (3) or fewer digits of fractional second. This limit is because of a legacy implementation used for Clock
.
Java 6 & 7
The old date-time classes first bundled with the earliest versions of Java support only milliseconds resolution. This includes java.util.Date
, java.util.Calendar
, and java.util.GregorianCalendar
. These old classes are also poorly designed, confusing, and troublesome. Avoid them.
Much of the java.time functionality is back-ported in the ThreeTen-Backport project, and further adapted to Android in the ThreeTenABP project.
As far as I know these rely upon the same legacy implementation, for only milliseconds resolution.
Accuracy
Accuracy and precision always depends on your host hardware clock capability.
See the Question, How precise is the internal clock of a modern PC?
As I understand today’s common computer hardware, you cannot expect the current moment will be captured with single nanosecond precision.

- 1
- 1

- 303,325
- 100
- 852
- 1,154
-
1Excellent summary. The main thing is that the system clock resolution is likely lower than a nanosecond. – Boris the Spider Jun 29 '16 at 19:45
-
Thanks a lot for that explanation. Its too bad that it captures only a millisecond resolution. This is a part of a small API that I am working on and I essentially require that precision. Sadly java spoils the deal for me. – Krish Jun 29 '16 at 21:25
-
@Krish If capturing current moment in finer than milliseconds is really that important, then consider pulling the `Clock` implementation from Java 9 pre-release code into your own app with your own `Clock` class. (If you can abide by its GPL license.) I don't know what is involved; you'll have to browse the OpenJDK source code. Note how the java.time classes allow for injecting a `Clock` object. [`Instant.now( Clock clock)`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#now-java.time.Clock-) – Basil Bourque Jun 29 '16 at 21:38
-
@Krish Another possibility is to call out to native code to access a platform-specific library to get the current moment from host OS. There are a few ways to call native code from Java, with JNI being one though I believe there is a newer easier way (don't know details). – Basil Bourque Jun 29 '16 at 21:44
-
@BasilBourque thanks for that. I was actually just looking into JNI. But I guess its probably not a bad idea to look at the java 9 pre-release. Thanks for that. Extremely appreciate the help. – Krish Jun 29 '16 at 21:48
-
1@Krish FYI… [Java Native Access(JNA)](https://en.wikipedia.org/wiki/Java_Native_Access) is the easier alternative to JNI. (Not that I've ever used either.) Also, you could combine these ideas, to make a `Clock` implementation that wraps calls to the native code call to the host OS clock library. Good luck. – Basil Bourque Jun 29 '16 at 22:33
If you want to get time in nano seconds means then use system.nanoTime(); this would be the best

- 70
- 6
-
3No. As explained in the documentation; `nanoTime` is nanos elapsed since JVM start. – Boris the Spider Jun 29 '16 at 18:22