Java 8 Instant.now() with nanosecond resolution?

冷暖自知 提交于 2019-11-26 06:43:35

问题


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?


回答1:


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.




回答2:


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

Instant may model the time to nanosecond precision, but the real-life equipment on your computer which tracks wall-clock time usually has resolution around 10 ms, and accuracy almost certainly quite a bit below that (100 ms would be an optimistic assumption).

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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/20689055/java-8-instant-now-with-nanosecond-resolution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!