为什么这段代码的时区输出错误?时间戳是正确的(凌晨2:30),但是为什么我要将CST作为时区格式呢?
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a (zz)")
.withZone(TimeZone.getTimeZone("Asia/Shanghai").toZoneId());
System.out.println(dateTimeFormatter.format(Instant.ofEpochMilli(Long.parseLong("1655490600000"))));发布于 2022-06-14 16:36:48
通常用于时区本地化表示的2-4字母伪区域没有标准化.他们甚至不是独一无二的,你似乎已经注意到了。
这些2-4字母代码仅用于对用户进行本地化表示。不要将它们用于数据存储或数据交换。
另一件事:不要使用TimeZone类。这是遗留日期-时间类之一。只使用java.time包中的日期时间类。专门由ZoneId和ZoneOffset代替。
在下面的代码中,Instant .ofEpochMilli返回一个Instant。atZone调用返回一个ZonedDateTime。
Instant
.ofEpochMilli(
Long
.parseLong( "1655490600000" )
)
.atZone(
ZoneId.of( "Asia/Shanghai" )
)
.toString()2022-06-18T02:30+08:00亚洲/上海
https://stackoverflow.com/questions/72620247
复制相似问题