How to get current time in nano seconds in java?

Viewed 18113

I do lots of research but did not find any good answer. I wanted to get the current date and time in a nanosecond. I found that System.nanoTime() will provide nanoseconds, but that is and system elapsed time. means it will provide a time when system up. I need to use the current date and time in a nanosecond.

I need this for avoiding duplicate of points in InfluxDB, see How does InfluxDB handle duplicate points? So when I use millisecond I am facing issues of data union. So need deciding to go with nanosecond but the problem is while generated nano the second using System.nanoTime() did not contain current date and time data. and it give me JVM uptime which is useless for me.

3 Answers

In theory, it is possible to get the current time to "better than microsecond" accuracy as follows:

Clock clock = Clock.systemDefaultZone();
Instant instant = clock.instant();   // or Instant.now();
long seconds = instant.getEpochSecond();
long nano = instant.getNano();
// epoch nanoseconds = seconds * 10E9 + nano

The problems:

  1. The systemDefaultZone() call gives the "best available clock" for the platform. The JVM spec says that this may have better than millisecond precision, but this is not guaranteed. So the nano value may have no better than millisecond precision.

  2. The values of seconds and nano depend on the accuracy of the local hardware clock. On many systems, keeping the local clock synced to a "real" time is difficult. Often, sub-millisecond accuracy is challenging, and apparent nanosecond accuracy is an illusion.

  3. Even you have previously managed to sync the hardware clock with a "real" time source to nanosecond accuracy, the overheads and variability in the making the above calls to acquire the epoch nanosecond time would swamp the hardware clock's nanosecond accuracy. Things like memory cache variability, how busy the main memory bus is, etc. And of course the hardware clock may have drifted since it was last synced externally.

In practice, on most systems, nano-second accuracy is unachievable, so you need avoid designs / algorithms that depend on this.

Finally, Thanks for Mr. Franz Wilhelmstötter

I found one solution. using http://jenetics.io/ and Class call NanoClock.java is converting and doing the same trick that Stephen C suggested. I want to share this because it will useful for others as well. I am not able to confirm that is given precise nano time, but this trick works for me. @Ole V.V. Thanks again for your help.

You get the best accuracy and precision Java can give you from Instant.now(). Whether this is enough to solve your problem, I dare not tell. Certainly on a normal computer there is no way to get nanosecond accuracy.

You may need to play some tricks with adding an artificial nanosecond in case Instant.now() returns the same value twice.

Or simple use the trick mentioned in your link:

Introduce an arbitrary new tag to enforce uniqueness.

For the trick of adding an artificial nanosecond you may for example use something like the following:

public class TimeProvider {

    Instant last = Instant.now().minusSeconds(1);

    Instant getUniqueInstant() {
        Instant result = Instant.now();
        if (! result.isAfter(last)) {
            result = last.plusNanos(1);
        }
        last = result;
        return result;
    }
}

When I draw times in rapid succession from this class on my computer, I get results like below. It would seem from the output (the way I interpret it):

  • My JVM cannot get higher precision than microseconds (6 decimals on the seconds) from the system clock.
  • An artificial nanosecond is added now and then to keep the instants unique.

.

2018-08-29T15:18:35.617616001Z
2018-08-29T15:18:35.617617Z
2018-08-29T15:18:35.617618Z
2018-08-29T15:18:35.617618001Z
2018-08-29T15:18:35.617619Z
2018-08-29T15:18:35.617619001Z
2018-08-29T15:18:35.617620Z
2018-08-29T15:18:35.617620001Z
2018-08-29T15:18:35.617621Z
2018-08-29T15:18:35.617621001Z
2018-08-29T15:18:35.617622Z
2018-08-29T15:18:35.617623Z
2018-08-29T15:18:35.617623001Z
2018-08-29T15:18:35.617624Z
2018-08-29T15:18:35.617624001Z
2018-08-29T15:18:35.617625Z
2018-08-29T15:18:35.617625001Z
2018-08-29T15:18:35.617626Z
2018-08-29T15:18:35.617626001Z
2018-08-29T15:18:35.617627Z
2018-08-29T15:18:35.617627001Z
2018-08-29T15:18:35.617628Z
2018-08-29T15:18:35.617631Z
2018-08-29T15:18:35.617634Z
2018-08-29T15:18:35.617635Z
2018-08-29T15:18:35.617636Z
2018-08-29T15:18:35.617636001Z
2018-08-29T15:18:35.617637Z
2018-08-29T15:18:35.617637001Z
2018-08-29T15:18:35.617638Z
Related