Hot questions for Using Joda-Time in jsr310

Question:

I'm looking for a solution to calculate the months between two date. I think joda or java8 time can do it. But when I compare them I found something really weird.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import org.joda.time.Months;

public class DateMain {
    public static void main(String[] args) throws ParseException {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date d1 = simpleDateFormat.parse("2017-01-28 00:00:00.000");
    Date d2 = simpleDateFormat.parse("2017-02-28 00:00:00.000");

    System.out.println("Test Cast 1");
    System.out.println("joda time api result: " + monthsBetweenJoda(d1, d2) + " month");
    System.out.println("java8 time api result: " + monthsBetweenJava8(d1, d2) + " month");

    Date dd1 = simpleDateFormat.parse("2017-01-29 00:00:00.000");
    Date dd2 = simpleDateFormat.parse("2017-02-28 00:00:00.000");

    System.out.println("Test Cast 2");
    System.out.println("joda time api result: " + monthsBetweenJoda(dd1, dd2) + " month");
    System.out.println("java8 time api result: " + monthsBetweenJava8(dd1, dd2) + " month");
}

public static int monthsBetweenJoda(Date fromDate, Date toDate) {
    if (fromDate == null || toDate == null) {
        throw new IllegalArgumentException();
    }
    org.joda.time.LocalDateTime fromLocalDateTime = org.joda.time.LocalDateTime
        .fromDateFields(fromDate);
    org.joda.time.LocalDateTime toLocalDateTime = org.joda.time.LocalDateTime
        .fromDateFields(toDate);
    Months months = Months.monthsBetween(fromLocalDateTime, toLocalDateTime);
    return months.getMonths();
}

public static long monthsBetweenJava8(Date fromDate, Date toDate) {
    if (fromDate == null || toDate == null) {
        throw new IllegalArgumentException();
    }
    LocalDateTime ldt1 = fromDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    LocalDateTime ldt2 = toDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    return ChronoUnit.MONTHS.between(ldt1, ldt2);
}

}

the output is below:

Test Cast 1
joda time api result: 1 month
java8 time api result: 1 month
Test Cast 2
joda time api result: 1 month
java8 time api result: 0 month

I feel very confused with the test case 2, and which one is reasonable?

Sorry, it's my first question here. Completed code attached.


Answer:

Joda-Time and Java 8 java.time.* have different algorithms for determining the number of months between.

java.time.* only declares a month has passed once the day-of-month is greater (or in the next month). From 2017-01-29 to 2017-02-28 it is clear that the second day-of-month (28) is less than the first (29) so a month has not yet been completed.

Joda-Time declares a month has passed because adding 1 month to 2017-01-29 will yield 2017-02-28.

Both are plausible algorithms, but I believe the java.time.* algorithm to be more in tune with what people expect (which is why I chose a different algorithm when writing java.time.*).

Question:

I can't seem to figure out why joda time is updating the time and offset hours after daylight saving time, but java time doesn't.

    DateTime dateTime = new DateTime("2016-04-05T10:06:21.636-05:00").withDayOfWeek(5);

    TemporalField dayOfWeek = WeekFields.ISO.dayOfWeek();
    OffsetDateTime offsetDateTime = OffsetDateTime.parse("2016-04-05T10:06:21.636-05:00").with(dayOfWeek, 5);
    ZonedDateTime zonedDateTime = ZonedDateTime.parse("2016-04-05T10:06:21.636-05:00").with(dayOfWeek, 5);

    System.out.println("dateTime:         " + dateTime); // 2016-04-08T11:06:21.636-04:00
    System.out.println("offsetDateTime:   " + offsetDateTime); // 2016-04-08T10:06:21.636-05:00
    System.out.println("zonedDateTime:    " + zonedDateTime); // 2016-04-08T10:06:21.636-05:00

Answer:

Time zone versus offset

You did not provide a time zone, only an offset to both, the offset date time and the zoned date time instances. In both cases, they don't have any clue about daylight saving times as this is an information of the time zone.

So you must provide a time zone when constructing the zoned date time object, and it then it works as you expect.

Question:

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

Answer:

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.