简体   繁体   English

java.time.Instant 是如何计算纳秒的?

[英]How are nanoseconds calculated in java.time.Instant?

The Java 8 docs on java.time.Instant state: 关于 java.time.Instant状态的Java 8 文档

The range of an instant requires the storage of a number larger than a long.瞬间的范围需要存储一个大于 long 的数字。 To achieve this, the class stores a long representing epoch-seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999.为了实现这一点,该类存储了一个表示纪元秒的 long 和一个表示纳秒秒的 int,它们总是介于 0 和 999,999,999 之间。

As per the System.currentTimeMillis() documentation , the Java 8 clock only guarantees, at best, millisecond resolution:根据System.currentTimeMillis() 文档,Java 8 时钟最多只能保证毫秒分辨率:

Note that while the unit of time of the return value is a millisecond, the accuracy of the value depends on the underlying operating system and may be larger注意,虽然返回值的时间单位是毫秒,但准确度取决于底层操作系统,可能会更大

If Java 8 only guarantees millisecond accuracy at best, how does Java calculate the java.time.Instant nanosecond-of-second value?如果 Java 8 最多只能保证毫秒精度,那么 Java 如何计算java.time.Instant纳秒级值?

The Instant class just promises nanosecond precision (ie the value of nanos will be provided in nanoseconds ).Instant类只是承诺纳秒的精度(即价值nanos在纳秒提供)。 It does not promise nanosecond accuracy (ie the value may not accurately represent the current number of nanoseconds).并不保证纳秒精度(即该值可能不准确地表示纳秒的当前数目)。

A simple multiplier (as specified by the specific SystemClock class instance used) is all that is needed to convert whatever unit can be obtained to nanoseconds.只需一个简单的乘数(由所使用的特定SystemClock类实例指定)即可将任何可以获得的单位转换为纳秒。


Other points:其他要点:

The System.currentTimeMillis method's documentation you link does not say 'at best';您链接的System.currentTimeMillis方法的文档没有说“充其量”; it just promises to do as well as it can.它只是承诺尽其所能。

Secondly, you can see the implementation of the method by following through the stack trace in (for example) Instant.now() .其次,您可以通过(例如) Instant.now()的堆栈跟踪来查看该方法的实现。 On my machine's java 8 setup (Mac OS, JDK 1.8.0.241) this falls back to using the SystemClock class, which eventually uses System.currentTimeMillis() and Instant.ofEpochMillis .在我机器的 java 8 设置(Mac OS,JDK 1.8.0.241)上,这回退到使用SystemClock类,最终使用System.currentTimeMillis()Instant.ofEpochMillis In that method, you can see that it simply multiplies the 'epoch millis' by 1_000_000 .方法中,您可以看到它只是将 'epoch millis' 乘以1_000_000

This has changed in later versions of java however.然而,这在 Java 的更高版本中发生了变化。

Java uses Clock class to get current instant. Java 使用Clock类来获取当前时刻。 Clock.systemUTC() return best available system clock instance which could return more precise timestamp. Clock.systemUTC()返回最佳可用系统时钟实例,它可以返回更精确的时间戳。

That said, Java 8 on Windows uses System.currentTimeMillis() as a source so it returns millisecond-precise time.也就是说,Windows 上的 Java 8 使用System.currentTimeMillis()作为源,因此它返回毫秒级精确时间。 But that's not guaranteed and other implementations can provide more precise time.但这并不能保证,其他实现可以提供更精确的时间。

Also note that Instant is not necessarily obtained by Instant.now() call.另请注意, Instant不一定通过Instant.now()调用获得。 You can parse it from input data, for example, where nanosecond precision might be useful.例如,您可以从输入数据中解析它,其中纳秒精度可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM