Java Reference
In-Depth Information
// Derive the local date and time from teh localdatetime
LocalDate ld2 = LocalDate.from(ldt2);
LocalTime lt2 = LocalTime.from(ldt2);
System.out.println("ld2: " + ld2);
System.out.println("lt2: " + lt2);
}
}
ld: 2012-05-11
ld.isLeapYear(): true
lt: 08:52:23
ldt: 2012-05-11T08:52:23
ldt2: 2012-07-11T09:17:23
ld2: 2012-07-11
lt2: 09:17:23
You can add years, months, and days to a LocalDate . What would be the result if you add one month to 2014-01-31? If
the date-Time API simply adds the month to the month field, the result would be 2014-02-31, which is an invalid date.
After adding the month, the result is checked if it is a valid date. If it is not a valid date, the day of month is adjusted to
the last day of the month. In this case, the result would be 2014-02-28.
LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 31);
LocalDate ld2 = ld1.plusMonths(1);
System.out.println(ld1);
System.out.println(ld2);
2014-01-31
2014-02-28
If you add days to a LocalDate, the month and year fields are adjusted to keep the result a valid date.
LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 31);
LocalDate ld2 = ld1.plusDays(30);
LocalDate ld3 = ld1.plusDays(555);
System.out.println(ld1);
System.out.println(ld2);
System.out.println(ld3);
2014-01-31
2014-03-02
2015-08-09
How do you compute number of days, hours, etc. between two dates and times? The Date-Time API has different
ways to compute the period between two dates and times. I will defer the discussion on such computations until the
section “Period Between Two Dates and Times.”
Search WWH ::




Custom Search