Java System.nanoTime() 时代?

Java epoch of System.nanoTime()?

我想知道 System.nanoTime() 的纪元是基于什么。 显然它不是基于 1970 年,因为 (System.nanoTime()/1000000L) (returns 31106704) no where near equals System.currentTimeMillis() (returns 1626111981278).

嗯,nanoTime() 的文档很清楚:

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine...

所以:

  • 纪元是实现特定的,正是方法实现的作者认为可行的;
  • 纪元在整个 JVM 实例中是固定的;
  • 可能其中一个或两个值为负,但在这种情况下计算仍然正确。

所以,你应该关心纪元吗?不,绝对不是。您对 的实现感到好奇吗?你不能,因为没有“the”实现。