该java.time
API通常 确实 具有纳秒级的精度。例如:
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ");
OffsetDateTime odt = OffsetDateTime.of(2015, 11, 2, 12, 38, 0, 123456789, ZoneOffset.UTC);
System.out.println(odt.format(formatter));
输出:
2015-11-02T12:38:00,123456789+0000
但是,它OffsetDateTime.Now()
是返回的时钟值,它返回的值只有毫秒。
从Clock
Java8的实现中:
此处提供的时钟实现基于System.currentTimeMillis()
。该方法几乎不能保证时钟的准确性。需要更精确时钟的应用程序必须使用其他外部时钟(例如NTP服务器)自己实现此抽象类。
因此,这里并没有天生的不精确之处-只是Clock
using 的默认实现System.currentTimeMillis()
。您可能会创建自己的更精确的子类。但是,您应该注意,增加精度而不增加 _精度_可能并不是非常有用。(诚??然,有时候可能会这样)