编程知识 cdmana.com

Java8 zoneddatetime date precision to nanoseconds

 1 second = 10E3 millisecond = 10E6 subtle   = 10E9 nanosecond

Use java8 Instant Internal reality System.currentTimeMillis() On the model Output nanosecond data The point is the model

Time stamp date  

public static ZonedDateTime ofInstant(Instant instant, ZoneId zone) {

        Objects.requireNonNull(instant, "instant");

        Objects.requireNonNull(zone, "zone");

        return create(instant.getEpochSecond(), instant.getNano(), zone);

    }

You can see in the source code establish ZonedDateTime Into instant Seconds time stamp instant.getEpochSecond() And nanosecond  instant.getNano()

See through breakpoints

The accuracy of the data is only given in milliseconds . That's why it was said at the beginning Instant It's actually given to the model in nanoseconds . actually os Level ordinary computer is unable to achieve nanosecond accuracy , Only with NTP Only time servers can .

So on the date output , On the model, it's nanosecond level , It's just that the actual data is only milliseconds So the output date is usually as follows :

2020-11-12T09:42:47.511+08:00[Asia/Shanghai]

We have to look at the nanoseconds

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX");

    String time =  ZonedDateTime.now().format(df);

    System.out.println(time);
   
result : 2020-11-12T09:41:03.413000000+08:00

版权声明
本文为[kolusiMken]所创,转载请带上原文链接,感谢

Scroll to Top