Get a Period of Days between two Dates
I believe this is the correct result. If you run the following test you will see that getMonths() returns 1. It seems that getDays is not get the total days but get the remainder days.
@Test
public void testOneMonthPeriodDays() {
Period p = Period.between(LocalDate.of(2017, 06, 01), LocalDate.of(2017, 07, 01));
assertEquals(0, p.getDays());
assertEquals(1, p.getMonths());
}
To get the total number of days between two dates see this question Calculate days between two dates in Java 8
To quote the answer given in that question:
LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);
Note that DAYS
is member of java.time.temporal.ChronoUnit
enum.
JavaDoc for java.time.Period#getDays()
says:
returns the amount of days of this period, may be negative
I guess this is missleading and it does not really return the total amount of days.
I use now
final long daysElapsed = ChronoUnit.DAYS.between(LocalDate.of(2017, 06, 01), LocalDate.of(2017, 07, 01));
which works as expected.