LocalDateTime.now() has different levels of precision on Windows and Mac machine

不羁的心 提交于 2021-01-19 14:05:05

问题


When creating a new LocalDateTime using LocalDateTime.now() on my Mac and Windows machine i get a nano precision of 6 on my Mac and a nano precision of 3 on my Windows machine. Both are running jdk-1.8.0-172.

  • Is it possible to limit or increase the precision on one of the machines?
  • And why is the precision actually different?

回答1:


The precision is different because LocalDateTime.now() uses a system default Clock.

Obtains the current date-time from the system clock in the default time-zone.

This will query the system clock in the default time-zone to obtain the current date-time.

...

The link in this Javadoc takes you to Clock.systemDefaultZone() which states (emphasis mine):

Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.

This clock is based on the best available system clock. This may use System.currentTimeMillis(), or a higher resolution clock if one is available.

...

Which clock Java uses can depend on a lot of things and it looks like your Mac computer has a clock with microsecond precision whereas your Windows computer has a clock with millisecond precision. I'm not aware of any way to increase the precision of a clock but you can definitely decrease the precision so that it matches across platforms.

One option is to do as Ole V.V. does in his answer and use LocalDateTime.truncatedTo(TemporalUnit).

Another option is to plug in your own Clock and use LocalDateTime.now(Clock). If possible, I would use Clock.tickMillis(ZoneId) since this method returns a Clock that truncates to milliseconds.

Obtains a clock that returns the current instant ticking in whole milliseconds using the best available system clock.

This clock will always have the nano-of-second field truncated to milliseconds. This ensures that the visible time ticks in whole milliseconds. The underlying clock is the best available system clock, equivalent to using system(ZoneId).

...

Since:
9




回答2:


I don’t think you can get any better precision than the one you are already getting. If you want to reduce the precision to match that of the other system, it’s straightforward (when you know how):

LocalDateTime.now(ZoneId.of("Europe/Berlin")).truncatedTo(ChronoUnit.MILLIS);

The precision you get depends on the hardware, the settings, the OS and the integration of the JVM with all of those. It’s well known that Mac generally offers better precision than Windows (though I was under the impression that this was only the case from Java 9, per OpenJDK issue # JDK‑8068730).




回答3:


You can set the precision using a formatter, without resorting to truncatedTo or Clock.tickMillis:

jshell> OffsetDateTime.now().format(ISO_DATE_TIME)
$5 ==> "2020-10-21T10:13:48.57776451+02:00"

jshell> OffsetDateTime.now().format(ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSZ"))
$8 ==> "2020-10-21T10:15:31.27+0200"

I realized this after discovering that JDK 15 has different precision on macOS and Linux.



来源:https://stackoverflow.com/questions/52029920/localdatetime-now-has-different-levels-of-precision-on-windows-and-mac-machine

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