Java8时api如何选择夏令时变化周期的偏移量
How does Java 8 time api choose the offset on DST change period
在Java8时API,可以创建一个LocalDateTime,落在秋季DST时间变化(中欧时间)的时间重叠。
您可以使用 ZoneId 将其转换为代表精确时间的 ZonedDateTime。
这样做实际上并不能解决歧义 - 仍然可能有两个时刻对应于此 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:
中有详细记录
In most cases, there is only one valid offset for a local date-time. In the case of an overlap, when clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to "summer".
如@JodaStephen 的评论所述,如果您想选择其他可用的偏移量,可以使用 the withLaterOffsetAtOverlap
method。
在Java8时API,可以创建一个LocalDateTime,落在秋季DST时间变化(中欧时间)的时间重叠。
您可以使用 ZoneId 将其转换为代表精确时间的 ZonedDateTime。
这样做实际上并不能解决歧义 - 仍然可能有两个时刻对应于此 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:
中有详细记录In most cases, there is only one valid offset for a local date-time. In the case of an overlap, when clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to "summer".
如@JodaStephen 的评论所述,如果您想选择其他可用的偏移量,可以使用 the withLaterOffsetAtOverlap
method。