1

以欧洲芬兰赫尔辛基为例,夏令时结束时间为Sunday, 2017 October 29, 4:00 am

我正在寻找一种方法来创建具有正确 DST 设置ZonedDateTime的实例(见下文),希望通过现有的工厂静态方法。

ZonedDateTime beforeDst = ZonedDateTime.of(2017, 10, 29, 3, 59, 0, 0, ZoneId.of("Europe/Helsinki"));
// this will print: 2017-10-29T03:59+03:00[Europe/Helsinki]

beforeDst.plusMinutes(1);
// this will print: 2017-10-29T03:00+02:00[Europe/Helsinki]
// note the +3 become +2, and 3:59 become 3:00

问题:我们如何创建ZonedDateTime将打印的内容2017-10-29T03:00+02:00

  • 希望通过将 LocalDateTime 作为参数或上面示例的参数传递,
  • 没有加/删除日期操作,和
  • 字符串解析不是一个选项(否java.time.format.DateTimeFormatter)。
4

2 回答 2

3

Please read the documentation, i.e. the javadoc of ZonedDateTime:

For Overlaps, the general strategy is that if the local date-time falls in the middle of an Overlap, then the previous offset will be retained. If there is no previous offset, or the previous offset is invalid, then the earlier offset is used, typically "summer" time.. Two additional methods, withEarlierOffsetAtOverlap() and withLaterOffsetAtOverlap(), help manage the case of an overlap.

Code to show this for your example:

ZonedDateTime zdt = ZonedDateTime.of(2017, 10, 29, 3, 00, 0, 0, ZoneId.of("Europe/Helsinki"));
ZonedDateTime earlier = zdt.withEarlierOffsetAtOverlap();
ZonedDateTime later = zdt.withLaterOffsetAtOverlap();
System.out.println(zdt);
System.out.println(earlier); // unchanged
System.out.println(later);

Output

2017-10-29T03:00+03:00[Europe/Helsinki]
2017-10-29T03:00+03:00[Europe/Helsinki]
2017-10-29T03:00+02:00[Europe/Helsinki]
于 2017-04-28T06:15:26.340 回答
1

使用withLaterOffsetAtOverlap()方法:

ZonedDateTime zdt = ZonedDateTime.of(2017, 10, 29, 3, 0, 0, 0,
     ZoneId.of("Europe/Helsinki"));
zdt = zdt.withLaterOffsetAtOverlap();

System.out.println(zdt); // 2017-10-29T03:00+02:00[Europe/Helsinki]
于 2017-04-28T06:14:54.313 回答