[英]How does Java 8 time api choose the offset on DST change period
在Java 8时间API中,您可以创建一个LocalDateTime,它在秋季(中欧时间)DST时间更改期间的时间重叠。
您可以将其转换为ZonedDateTime,它表示使用ZoneId的精确时刻。
这样做实际上并没有解决模糊性 - 可能仍然有两个时刻对应于此LocalDateTime和此区域(但在不同的偏移处)。
如何以及为什么(参考欢迎)时间API选择夏季偏移量?
@Test
public void TimeSetOnDST() throws Exception {
LocalDateTime time = LocalDateTime.of(2016, 10, 30, 2, 30); // in the DST time overlap
ZonedDateTime of = ZonedDateTime.of(time, ZoneId.of("Europe/Zurich"));
System.out.println(of); // 2016-10-30T02:30+02:00[Europe/Zurich]
// But why not 2016-10-30T02:30+01:00[Europe/Zurich] ?
// Is this just "by convention"?
}
它在javadoc中有很好的文档:
在大多数情况下,本地日期时间只有一个有效的偏移量。 在重叠的情况下,当设置回时,有两个有效的偏移。 该方法使用通常对应于“夏天”的较早偏移。
如@JodaStephen的评论中所述,如果要选择其他可用偏移量,可以使用withLaterOffsetAtOverlap
方法 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.