为什么 JodaTime 库和 JDK OffsetDateTime 报告太平洋/诺福克时区偏移量不同?哪一个是正确的?

Raj*_*ami 1 java jodatime java-time

public static void main(String[] args) {
    System.out.println("Norfolk Island times as per Joda Time");
    IntStream.range(1, 13)
            .forEach(month -> System.out.println(DateTime.now((DateTimeZone.forID("Pacific/Norfolk")))
                    .withYear(2023)
                    .withMonthOfYear(month)
                    .withDayOfMonth(10)
                    .withHourOfDay(2)
                    .withMinuteOfHour(10)
                    .withSecondOfMinute(2)
                    .withMillisOfSecond(0)));
    System.out.println("-------------------------------------");
    System.out.println("Norfolk Island times as per JDK OffsetDateTime");
    IntStream.range(1, 13)
            .forEach(month -> System.out.println(OffsetDateTime.now(ZoneId.of("Pacific/Norfolk"))
                    .withYear(2023)
                    .withMonth(month)
                    .withDayOfMonth(10)
                    .withHour(2)
                    .withMinute(10)
                    .withSecond(2)
                    .withNano(0)));
    System.out.println("-------------------------------------");
}
Run Code Online (Sandbox Code Playgroud)

输出:

Norfolk Island times as per Joda Time
2023-01-10T02:10:02.000+12:00
2023-02-10T02:10:02.000+12:00
2023-03-10T02:10:02.000+12:00
2023-04-10T02:10:02.000+11:00
2023-05-10T02:10:02.000+11:00
2023-06-10T02:10:02.000+11:00
2023-07-10T02:10:02.000+11:00
2023-08-10T02:10:02.000+11:00
2023-09-10T02:10:02.000+11:00
2023-10-10T02:10:02.000+12:00
2023-11-10T02:10:02.000+12:00
2023-12-10T02:10:02.000+12:00
-------------------------------------
Norfolk Island times as per JDK OffsetDateTime
2023-01-10T02:10:02+11:00
2023-02-10T02:10:02+11:00
2023-03-10T02:10:02+11:00
2023-04-10T02:10:02+11:00
2023-05-10T02:10:02+11:00
2023-06-10T02:10:02+11:00
2023-07-10T02:10:02+11:00
2023-08-10T02:10:02+11:00
2023-09-10T02:10:02+11:00
2023-10-10T02:10:02+11:00
2023-11-10T02:10:02+11:00
2023-12-10T02:10:02+11:00
-------------------------------------
Run Code Online (Sandbox Code Playgroud)

因此,根据JodaTime,他们遵守 DST,但 OffsetDateTime 说他们不遵守。

哪一个是正确的?或者我做错了什么?

Java:JDK 1.8.0.311

Joda时间:2010年10月2日

Sai*_*mad 7

根据官方文档java.time.OffsetDateTime。OffsetDateTime 只是一个附加了时区偏移量的实例表示。它不遵守夏令时等完整的时区规则。

诺福克岛有夏令时 从October 2ndApril 3rd

您真正需要的是java.time.ZonedDateTime。如果您使用 ZonedDateTime 对象运行代码,您将得到类似的结果JodaTime

IntStream.range(1, 13)
    .forEach(month -> System.out.println(ZonedDateTime.now(ZoneId.of("Pacific/Norfolk"))
            .withYear(2023)
            .withMonth(month)
            .withDayOfMonth(10)
            .withHour(2)
            .withMinute(10)
            .withSecond(2)
            .withNano(0)));
Run Code Online (Sandbox Code Playgroud)

输出:

2023-01-10T02:10:02+12:00[Pacific/Norfolk]
2023-02-10T02:10:02+12:00[Pacific/Norfolk]
2023-03-10T02:10:02+12:00[Pacific/Norfolk]
2023-04-10T02:10:02+11:00[Pacific/Norfolk]
2023-05-10T02:10:02+11:00[Pacific/Norfolk]
2023-06-10T02:10:02+11:00[Pacific/Norfolk]
2023-07-10T02:10:02+11:00[Pacific/Norfolk]
2023-08-10T02:10:02+11:00[Pacific/Norfolk]
2023-09-10T02:10:02+11:00[Pacific/Norfolk]
2023-10-10T02:10:02+12:00[Pacific/Norfolk]
2023-11-10T02:10:02+12:00[Pacific/Norfolk]
2023-12-10T02:10:02+12:00[Pacific/Norfolk]
Run Code Online (Sandbox Code Playgroud)