【发布时间】:2016-10-12 08:07:19
【问题描述】:
在 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"?
}
【问题讨论】: