我现在正在巴黎运行这段代码,其中12:40,其中ECT = ECT – 欧洲/巴黎
LocalDateTime creationDateTime = LocalDateTime.now(Clock.systemUTC());
ZoneId zone = ZoneId.of(ZoneId.SHORT_IDS.get("ECT"));
System.out.println ("creationDateTime --------------------------> " + creationDateTime);
System.out.println ("creationDateTime.atZone(zone).getHour() ---> " + creationDateTime.atZone(zone).getHour());
System.out.println ("creationDateTime.atZone(zone).getMinute() -> " + creationDateTime.atZone(zone).getMinute());
但我在控制台得到这个
creationDateTime --------------------------> 2017-05-16T10:40:07.882
creationDateTime.atZone(zone).getHour() ---> 10
creationDateTime.atZone(zone).getMinute() -> 40
我不应该得到12:40 ???????
解决方法:
不,你不应该.您已经要求使用与您开始时相同但与特定时区相关联的LocalDateTime的ZonedDateTime.
This returns a ZonedDateTime formed from this date-time at the specified time-zone. The result will match this date-time as closely as possible. Time-zone rules, such as daylight savings, mean that not every local date-time is valid for the specified zone, thus the local date-time may be adjusted.
在这种情况下,不需要调整,因为2017-05-16T10:40:07.882确实发生在巴黎.
听起来你的错误就是创建一个LocalDateTime.你基本上已经说过“找出当前时间是什么,然后采取相同的本地日期和时间,但假装它在不同的时区.”
如果您的目标是获取区域中的当前时间,则根本不应该有LocalDateTime.只需使用:
ZonedDateTime zonedNow = ZonedDateTime.now(Clock.system(zone));
或(等效地)
ZonedDateTime zonedNow = Clock.systemUTC().instant().atZone(zone);
标签:java,java-8,java-time,java-date
来源: https://codeday.me/bug/20190611/1217499.html