1

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:

Result from online date calculator

flowjoe
  • 105
  • 8

2 Answers2

2

The online date calculator you have provided may have been implemented incorrectly. From my favorite time resource, timeanddate

From and including: Friday, May 19, 2017

To, but not including Saturday, July 13, 2019

Result: 785 days

It is 785 days from the start date to the end date, but not including the end date.

Or 2 years, 1 month, 24 days excluding the end date.

Or 25 month, 24 days excluding the end date.

This is identical to the response provided by thecalculatorsite.com

I don't have the implementation for your online calculator, but the discrepancy appears to occur because May is a 31-day month.

It looks to be calculating the day-difference before the month difference, using some mathematical assumption for month length.

The Java method uses epochday to calculate the distances between two days in case of underflow, rather than adding an arbitrary value to offset a month.

    if (totalMonths > 0 && days < 0) {
        totalMonths--;
        LocalDate calcDate = this.plusMonths(totalMonths);
        days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
    } else if (totalMonths < 0 && days > 0) {
        totalMonths++;
        days -= end.lengthOfMonth();
Community
  • 1
  • 1
Compass
  • 5,867
  • 4
  • 30
  • 42
0

I mean, it should be 24 days depending on how you do the calculations.

If you add a month to get to june 19th, and june has 30 days- then it's only 24 days between the two dates.

https://www.wolframalpha.com/input/?i=days+between+5%2F19%2F2017+and+7%2F13%2F2019

2 Years 1 month 24 days

The website you are checking it against is wrong, I can think of several ways that could happen depending on how they are implementing their date difference functions.

msEmmaMays
  • 1,073
  • 7
  • 7