Java 8中新的Date Time API的一个特性应该是纳秒精度.但是,当我将当前日期时间打印到控制台时,就像这样
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ");
System.out.println(OffsetDateTime.now().format(formatter));
Run Code Online (Sandbox Code Playgroud)
我只看到毫秒精度:2015-11-02T12:33:26,746000000 + 0100
操作系统似乎确实支持纳秒精度.当我通过终端打印当前日期时间
date -Ins
Run Code Online (Sandbox Code Playgroud)
我看到2015-11-02T12:33:26,746134417 + 0100
如何在Java中获得纳秒精度?我在Ubuntu 14.04 64位上运行Oracle Java 1.8.0_66
您能否详细说明如何获得默认系统时区和给定时区的正确纪元时间(以毫秒为单位).
1. TimeZone:GMT + 3
2.以下代码段:
import java.time.*;
public class Main {
public static void main(String[] args) {
System.out.println(LocalDateTime
.now()
.atZone(ZoneOffset.UTC)
.toInstant()
.toEpochMilli()
);
System.out.println(LocalDateTime
.now()
.atZone(ZoneOffset.of("+3"))
.toInstant()
.toEpochMilli()
);
System.out.println(System.currentTimeMillis());
}
}
Run Code Online (Sandbox Code Playgroud)
3.输出:
1444158955508
1444148155508
1444148155508
Run Code Online (Sandbox Code Playgroud)
4.用于System.currentTimeMillis()的 JavaDoc,它告诉返回的值将是当前时间与1970年1月1日UTC午夜之间的差值(以毫秒为单位).
LocalDateTimeat 的输出GMT+3是相同的System.currentTimeMillis(),虽然System.currentTimeMillis()提到的文件UTC?LocalDateTimeat 的输出UTC不同System.currentTimeMillis(),虽然System.currentTimeMillis()提到的文件UTC?