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

后端 未结 3 642
陌清茗
陌清茗 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:26

    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.

提交回复
热议问题