La *_*ell 4 java java-8 java-time java-date
我现在正在巴黎运行这段代码,其中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());
Run Code Online (Sandbox Code Playgroud)
但我在控制台得到这个
creationDateTime --------------------------> 2017-05-16T10:40:07.882
creationDateTime.atZone(zone).getHour() ---> 10
creationDateTime.atZone(zone).getMinute() -> 40
Run Code Online (Sandbox Code Playgroud)
我不应该得到12:40 ???????
不,你不应该.您已经要求使用ZonedDateTime与LocalDateTime您开始时相同但与特定时区相关联的内容.
来自以下文档LocalDateTime.atZone:
这将返回在指定时区的此日期时间形成的ZonedDateTime.结果将尽可能与此日期时间匹配.时区规则(例如夏令时)意味着并非每个本地日期时间对指定区域都有效,因此可以调整本地日期时间.
在这种情况下,不需要调整,因为2017-05-16T10:40:07.882 确实发生在巴黎.
听起来你的错误就是创造一个LocalDateTime.你基本上已经说过"找出当前时间是什么,然后采取相同的本地日期和时间,但假装它在不同的时区."
如果您的目标是获得当前时间zone,那么您根本就不应该拥有LocalDateTime.只需使用:
ZonedDateTime zonedNow = ZonedDateTime.now(Clock.system(zone));
Run Code Online (Sandbox Code Playgroud)
或(等效地)
ZonedDateTime zonedNow = Clock.systemUTC().instant().atZone(zone);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
929 次 |
| 最近记录: |