what is the most precise time I can get in JDK?

David Holmes David.Holmes at oracle.com
Wed Nov 16 07:23:18 UTC 2011


Hi Max,

On 16/11/2011 2:55 PM, Weijun Wang wrote:
> I need a precise time, and is currently using java.util.Date, which knows
> about milliseconds, but unfortunately the precision is only 10-15
> milliseconds on a Windows.
>
> In fact, I don't really need it to be so correct. My requirements are:
>
> 1. It's enough correct, say, at least as correct as Date.
> 2. It's precise in a relative sense, i.e. it changes fast
> 3. It should be monotonic, i.e. it grows, unless the user adjusts the system
> clock

There are only two time source available:

1. The time-of-day clock

This is what Date reports and is also what System.currentTimeMillis reports. 
It only has millisecond precision. It's rate of update is dependent on the 
OS - for Windows that is typically every 10ms or every 15ms depending on 
version.

2. The high resolution time source

This is what System.nanoTime reports. It has nanosecond precision, but again 
depending on the OS it's resolution (update rate) will vary. The update rate 
should easily be in the tens of microseconds. It should be monotonic 
non-decreasing but it is not connected to the time-of-day clock (and so 
should not be affected by any changes therein).

I have an old blog entry on this:

http://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks

David
-----



More information about the core-libs-dev mailing list