gpt4 book ai didi

java - 基于 LocalDateTime 创建 ZonedDateTime 实例时出现问题

转载 作者:行者123 更新时间:2023-12-02 11:04:15 25 4
gpt4 key购买 nike

我看到了一些我无法解释的行为,希望有人能给我指出正确的方向。

我首先创建一个 LocalDateTime 实例,然后通过将“欧洲/伦敦”时区应用于 LocalDateTime 来获取 ZonedDateTime,然后使用 DateTimeFormatter 打印到输出。我希望看到与 LocalDateTime 实例中指定的时间完全相同的时间,但在下面的示例中,zonedDateTimeBeforeDST_Original 的输出显示比我预期晚一小时。

所以,简而言之:为什么我的 LocalDateTime 中的 01:55 变成了 ZonedDateTime 中的 02:55?

仅供引用,其他时区没有看到此行为。伦敦的似乎是一个特例。

    //init date pattern
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");

LocalDateTime localDateTimeBeforeDST = LocalDateTime.parse("2018-03-25 01:55:00", formatter); //testing with time 5 minutes before DST switch or after
System.out.println("LocalDateTime: " + localDateTimeBeforeDST.format(formatter));
System.out.println();

String originalZone = "Europe/London";

ZoneId originalZoneId = ZoneId.of(originalZone);
ZoneId utcZoneId = ZoneId.of("Etc/UTC");
ZoneId localZoneId = ZoneId.of("Europe/London");
ZoneId localZoneId_2 = ZoneId.of("Europe/Brussels");

ZonedDateTime zonedDateTimeBeforeDST_Original = localDateTimeBeforeDST.atZone(originalZoneId);
ZonedDateTime zonedDateTimeBeforeDST_UTC = zonedDateTimeBeforeDST_Original.withZoneSameInstant(utcZoneId);
ZonedDateTime zonedDateTimeBeforeDST_1 = zonedDateTimeBeforeDST_UTC.withZoneSameInstant(localZoneId);
ZonedDateTime zonedDateTimeBeforeDST_2 = zonedDateTimeBeforeDST_UTC.withZoneSameInstant(localZoneId_2);

System.out.println("Instant: " + zonedDateTimeBeforeDST_Original.toEpochSecond());
System.out.println();
System.out.println("ZonedDateTime (" + originalZone + "): " + zonedDateTimeBeforeDST_Original.format(formatterOut));
System.out.println("ZonedDateTime (Etc/UTC): " + zonedDateTimeBeforeDST_UTC.format(formatterOut));
System.out.println("ZonedDateTime (Europe/London): " + zonedDateTimeBeforeDST_1.format(formatterOut));
System.out.println("ZonedDateTime (Europe/Brussels): " + zonedDateTimeBeforeDST_2.format(formatterOut));
System.out.println();

zonedDateTimeBeforeDST_Original = zonedDateTimeBeforeDST_Original.plus(10, ChronoUnit.MINUTES);
zonedDateTimeBeforeDST_UTC = zonedDateTimeBeforeDST_Original.withZoneSameInstant(utcZoneId);
zonedDateTimeBeforeDST_1 = zonedDateTimeBeforeDST_UTC.withZoneSameInstant(localZoneId);
zonedDateTimeBeforeDST_2 = zonedDateTimeBeforeDST_UTC.withZoneSameInstant(localZoneId_2);

System.out.println("ZonedDateTime (DST) (" + originalZone + "): " + zonedDateTimeBeforeDST_Original.format(formatterOut));
System.out.println("ZonedDateTime (DST) (Etc/UTC): " + zonedDateTimeBeforeDST_UTC.format(formatterOut));
System.out.println("ZonedDateTime (DST) (Europe/London): " + zonedDateTimeBeforeDST_1.format(formatterOut));
System.out.println("ZonedDateTime (DST) (Europe/Brussels): " + zonedDateTimeBeforeDST_2.format(formatterOut));

输出:

LocalDateTime: 2018-03-25 01:55:00

Instant: 1521942900

ZonedDateTime (Europe/London): 2018-03-25 02:55:00 +01:00
ZonedDateTime (Etc/UTC): 2018-03-25 01:55:00 Z
ZonedDateTime (Europe/London): 2018-03-25 02:55:00 +01:00
ZonedDateTime (Europe/Brussels): 2018-03-25 03:55:00 +02:00

ZonedDateTime (DST) (Europe/London): 2018-03-25 03:05:00 +01:00
ZonedDateTime (DST) (Etc/UTC): 2018-03-25 02:05:00 Z
ZonedDateTime (DST) (Europe/London): 2018-03-25 03:05:00 +01:00
ZonedDateTime (DST) (Europe/Brussels): 2018-03-25 04:05:00 +02:00

最佳答案

这是夏令时 (DST) 问题。当英国开始夏令时,时钟从 1 拨到 2 时,没有 2018-03-25 01:55:00。 ZonedDateTime 不会为您提供不存在的时间,因此它选择了一个现有的时间。

具体选择的时间在 the documentation of LocalDateTime.atZone 中指定。 :

In the case of a gap, where clocks jump forward, there is no valid offset. Instead, the local date-time is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the local date-time will be moved one hour later into the offset typically corresponding to "summer".

当然,在使用夏令时的其他时区也会发生同样的情况。只有每个时区都有自己的转换时间,通常与 2018 年 3 月 25 日星期日 01:00 不一致。

关于java - 基于 LocalDateTime 创建 ZonedDateTime 实例时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51100503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com