java zoneid_java – ZoneId和LocalDateTime

我现在正在巴黎运行这段代码,其中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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值