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实现在Java 8:
The clock implementation provided here is based on System.currentTimeMillis(). That method provides little to no guarantee about the accuracy of the clock. Applications requiring a more accurate clock must implement this abstract class themselves using a different external clock, such as an NTP server.
所以这里没有什么固有的不精确 – 只是使用System.currentTimeMillis()的Clock的默认实现。你可以创建自己更精确的子类。然而,您应该注意,添加更多的精度,而不添加更多的精度可能是不是非常有用。 (有时,它可能是,诚然…)