Forum Stats

  • 3,838,492 Users
  • 2,262,373 Discussions
  • 7,900,671 Comments

Discussions

Java 11 Instant.now() gives microsecond precision but we need nanosecond

db019464-cd05-4426-b3ec-fe843a576450
edited Sep 26, 2018 11:15AM in Java Programming

As Java 11 is released, we got it and tried to print Instant.now(). Our requirement is to give the time string in nanosecond precision.

We are spiking on AWS with centos7, we verified that system clock has precision upto nanos by firing

$ date +"%s %9N" 1537941601 130559008

But while printing Instant.now() with following formatter

DateTimeFormatter.ISO_INSTANT.withZone(ZoneOffset.UTC).format(instant)

It gives

2018-09-26T05:57:18.390390Z

and with following formatter

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSSSSSSSS]").withZone(ZoneOffset.UTC)

it gives

2018-09-26T06:07:38.296302000

So, is that fair to say that support for nano precision is not present in java 11?

jwenting

Answers

  • Unknown
    edited Sep 26, 2018 11:15AM
    As Java 11 is released, we got it and tried to print Instant.now(). Our requirement is to give the time string in nanosecond precision.

    Ain't gonna happen. Your OS doesn't even have that precision.

    We are spiking on AWS with centos7, we verified that system clock has precision upto nanos by firing

    What do you mean by 'spiking'? AWS is a web service so don't know how you would 'spike' on it.

    What you verified is the the RESOLUTION is nanos - that is NOT the same as precision.

    Precision is accuracy relative to a known, fixed point in time. Resolution is the granularity of the result.

    Java's System.nanoTime has nano resolution but not nano accuracy since there is no fixed point in time to base it on.

    So, is that fair to say that support for nano precision is not present in java 11?

    Yep - that isn't present much of anywhere in the computer world.

    jwenting
This discussion has been closed.