我有这个毫秒长:
1570046362841
使用版本 1 进行转换时:
var myDate = ZonedDateTime.ofInstant(Instant.ofEpochSecond(1570046362841), ZoneId.of("America/New_York"));
我会得到这个结果(这是错误的!):
+51722-10-16T03:58:54-04:00[America/New_York]
但是,使用版本 2 进行转换时:
final String dateFormat = "yyyy-MM-dd HH:mm:ss SSS";
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
var dateObj = new Date(1570046362841);
var myDate = formatter.format(dateObj);
我得到了正确的结果:
2019-10-02 15:59:59 934
为什么版本 1 是错误的?版本 1 有什么问题?
最佳答案
您需要使用 Instant.ofEpochMilli()
而不是 Instant.ofEpochSecond()
因为您有毫秒。
关于java - 将 millis 转换为 ZonedDateTime 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58215382/