I am trying to calculate the difference between two LocalDate objects and the result seems to be off, but not every time.
I am using the Period construct. The below code shows one example which returns the expected result (I got that here) and another one which gives me the "wrong" result. I put that in quotes because I am not sure if that truly is wrong, or if the expected value is wrong. Note however, that if I use the online calculator from calculator.net, that gives me the result I expect.
public void manualTestPeriodBetween() {
//works fine - expected result obtained
LocalDate start = LocalDate.of(2014, 2, 14);
LocalDate end = LocalDate.of(2017, 8, 1);
Period result = Period.between(start, end);
Period expected = Period.of(3, 5, 18);
checkPeriods(expected, result);
//does not work as expected
start = LocalDate.of(2017, 5, 19);
end = LocalDate.of(2019, 7, 13);
result = Period.between(start, end);
expected = Period.of(2, 1, 25);
checkPeriods(expected, result);
}
private void checkPeriods(Period expected, Period result) {
System.out.println("expected Period = " + expected + ", resulting Period = " + result);
if (result.equals(expected)) {
System.out.println("SUCCESS - result Period matches expected");
} else {
System.out.println("FAIL - result Period not matched");
}
}
Output:
expected Period = P3Y5M18D, resulting Period = P3Y5M18D
SUCCESS - result Period matches expected
expected Period = P2Y1M25D, resulting Period = P2Y1M24D
FAIL - result Period not matched
Can someone help me figure out whether I am missing something or the expected result is wrong (both in my code and the online date calculator)? or maybe something else I am not even considering.
Here is a screenshot of the results obtained from the online date calculator: