Jon
Jon

Reputation: 453

Why is withWeekOfWeekyear giving me a different offset?

I'm trying to convert withWeekOfWeekyear over to java.time. I can't seem to figure out why I'm getting a different offset with withWeekOfWeekyear compared to weekOfWeekBasedYear.

    DateTime dateTimeWeek = new DateTime().withWeekOfWeekyear(1);
    OffsetDateTime offsetDateTimeWeek = OffsetDateTime.now().with(WeekFields.ISO.weekOfWeekBasedYear(), 1);

    DateTime dateTime = new DateTime();
    OffsetDateTime offsetDateTime = OffsetDateTime.now();

    System.out.println(dateTimeWeek); // 2016-01-04T12:20:50.981-05:00
    System.out.println(offsetDateTimeWeek); // 2016-01-04T12:20:51.034-04:00

    System.out.println(dateTime); // 2016-07-18T12:20:51.101-04:00
    System.out.println(offsetDateTime); // 2016-07-18T12:20:51.101-04:00

Upvotes: 1

Views: 178

Answers (1)

Andreas
Andreas

Reputation: 159114

org.joda.time.DateTime is time-zone aware.

A DateTime calculates its fields with respect to a time zone.

java.time.OffsetDateTime is not time-zone aware, i.e. does not adjust for Daylight Savings Time.

OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds full time-zone rules.

java.time.ZonedDateTime is time-zone aware, so if you use that, you should get same result.

ZonedDateTime is an immutable representation of a date-time with a time-zone.

Upvotes: 1

Related Questions