Why JodaTime Library and JDK OffsetDateTime report Pacific/Norfolk time zone offsets as different? Which one is correct?

Viewed 70
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("-------------------------------------");
}

Output:

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
-------------------------------------

So as per JodaTime, they observe DST, but OffsetDateTime says they do not.

Which one is correct? Or I am doing somthing wrong?

Java: JDK 1.8.0.311

JodaTime: 2.10.10

1 Answers

According to the official documentation java.time.OffsetDateTime. OffsetDateTime is just an Instance representation with a time-zone offset attached to it. It doesn't adhere to the full time-zone rules like Daylight savings.

Norflok Island does have daylight savings from October 2nd to April 3rd

What you actually need is java.time.ZonedDateTime. If you run your code with the ZonedDateTime object you'll get similar results to 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)));

OUTPUT:

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]
Related