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

后端 未结 3 659
陌清茗
陌清茗 2021-01-17 10:45

When creating a new LocalDateTime using LocalDateTime.now() on my Mac and Windows machine i get a nano precision of 6 on my Mac an

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-17 11:41

    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).

提交回复
热议问题