Java 8 Instant.now() with nanosecond resolution?

68.5k views Asked by At

Java 8's java.time.Instant stores in "nanosecond resolution", but using Instant.now() only provides millisecond resolution...

Instant instant = Instant.now();
System.out.println(instant);
System.out.println(instant.getNano());

Result...

2013-12-19T18:22:39.639Z
639000000

How can I get an Instant whose value is 'now', but with nanosecond resolution?

5

There are 5 answers

1
Meno Hochschild On

You can only get an Instant with "nanoseconds" by using another more precise java.time.Clock by using the Instant-method public static Instant now(Clock clock) In your example the default clock normally uses System.currentTimeMillis() which cannot display any nanoseconds.

Be aware that there is no clock available in nanosecond resolution (real time). The internal nanosecond representation facility of java.time.Instant is mainly due to the requirement to map database timestamps given in nanosecond precision (but normally not accurate to nanoseconds!).

Update from 2015-12-29: Java-9 will deliver a better clock, see my newer post.

9
Marko Topolnik On

You can consider yourself lucky if you get even millisecond resolution.

Instant may model the time to nanosecond precision, but as for the actual resolution, it depends on the underlying OS implementation. On Windows, for example, the resolution is pretty low, on the order of 10 ms.

Compare this with System.nanoTime(), which gives resolution in the microseconds, but doesn't give absolute wall-clock time. Clearly, there is already a tradeoff at work to give you that kind of resolution, still three orders of magnitude short of nanoseconds.

0
StormeHawke On

So I spent some time digging through the Javadoc here:

http://download.java.net/jdk8/docs/api/java/time/Instant.html

It appears that you should be able to do the following:

Instant inst = Instant.now();
long time = inst.getEpochSecond();
time *= 1000000000L; //convert to nanoseconds
time += inst.getNano(); //the nanoseconds returned by inst.getNano() are the nanoseconds past the second so they need to be added to the epoch second

That said - the other answerers make a good point that it's going to be mighty hard to get an accurate nano-second time as computers just don't typically have the capacity to track time to that resolution

1
Dave The Dane On

I needed a high-precision timer common to multiple JVM Instances.

As the Javadoc for System.nanoTime() explicitly states that each JVM is likely to use a different origin for this value, I created the following Test Class which calculates a Nanosecond offset to the Epoch so I can get a Timestamp with the precision of System.nanoTime() but a common origin. (That's basically the same idea proposed in the NanoClock solution posted by @logtwo)

Maybe it helps to visualise the differences in resolution between the values returned by Instant.now() & System.nanoTime() although both nominally have nanosecond precision.

On the Hardware I'm using this may cause deltas of about 1 Millisecond for the origin between JVM's, but that was more than adequate for my purposes.

import java.time.*;
import java.util.concurrent.TimeUnit;

public class EpochNanotimeOffset {

    private static final long NANO_TIME_EPOCH_OFFSET;
    /**/    static {
        final long     systemNanoTime         = System.nanoTime();
        final Instant  now                    = Clock.systemUTC().instant();
        final long     instantNanosSinceEpoch = Duration.between(Instant.EPOCH, now).toNanos();

        NANO_TIME_EPOCH_OFFSET = systemNanoTime - instantNanosSinceEpoch;
    }

    public static void main(final String[] args) throws InterruptedException {
        for (int i=0; i < 99; i++) {

            final long    systemNanoTime          = System.nanoTime();
            final Instant instant                 = Clock.systemUTC().instant();
            final long    instantNanos            = Duration.between(Instant.EPOCH, instant).toNanos();
            final long    correctedSystemNanoTime = systemNanoTime - NANO_TIME_EPOCH_OFFSET;
            final long    deltaNanos              = instantNanos   - correctedSystemNanoTime;

            Duration.between(Instant.EPOCH, instant).toNanos();

            System.out.print  (                  "OffsetNS=" + NANO_TIME_EPOCH_OFFSET );
            System.out.print  ('\t' + "instantNSsinceEpoch=" + instantNanos );
            System.out.print  ('\t' +   "correctedSystemNS=" + correctedSystemNanoTime );
            System.out.print  ('\t' +            "systemNS=" + systemNanoTime );
            System.out.println('\t' +             "deltaNS=" + deltaNanos );

//          TimeUnit.SECONDS.sleep(3);
        }
    }
}
10
logtwo On

While default Java8 clock does not provide nanoseconds resolution, you can combine it with Java ability to measure time differences with nanoseconds resolution, thus creating an actual nanosecond-capable clock.

public class NanoClock extends Clock
{
    private final Clock clock;

    private final long initialNanos;

    private final Instant initialInstant;

    public NanoClock()
    {
        this(Clock.systemUTC());
    }

    public NanoClock(final Clock clock)
    {
        this.clock = clock;
        initialInstant = clock.instant();
        initialNanos = getSystemNanos();
    }

    @Override
    public ZoneId getZone()
    {
        return clock.getZone();
    }

    @Override
    public Instant instant()
    {
        return initialInstant.plusNanos(getSystemNanos() - initialNanos);
    }

    @Override
    public Clock withZone(final ZoneId zone)
    {
        return new NanoClock(clock.withZone(zone));
    }

    private long getSystemNanos()
    {
        return System.nanoTime();
    }
}

Using it is straightforward: just provide extra parameter to Instant.now(), or call Clock.instant() directly:

    final Clock clock = new NanoClock();   
    final Instant instant = Instant.now(clock);
    System.out.println(instant);
    System.out.println(instant.getNano());

Although this solution might work even if you re-create NanoClock instances every time, it's always better to stick with a stored clock initialized early in your code, then used wherever it's needed.