Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8211989

MONTHS.between() or YEARS.between() returns wrong value

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux

      Description

      ADDITIONAL SYSTEM INFORMATION :
      The bug is reproducible on Linux and Windows OS on Java version 11 and 8. There is no known version of Java, where the returned result is as expected.

      A DESCRIPTION OF THE PROBLEM :
      When the first parameter (LocalDate) is the last day of February in a leap year (e.g. 2020.2.29) and second parameter last day of February in a non-leap year (e.g. 2021.2.28), method Months.between() return 11 and YEARS.between returns 0, although, in my opinion, it is the full year in between.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      To reproduce, simply just calculate the number of months/years between two dates (using LocalDate) where the first parameter is last of February on leap year and the second parameter is last day of February on a non-leap year.
      MONTHS.between(Temporal, Temporal);
      YEARS.between(Temporal, Temporal);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      For: System.out.println(MONTHS.between(LocalDate.of(2020, 2, 29), LocalDate.of(2021, 2, 28))); expected result is 12.
      For: System.out.println(YEARS.between(LocalDate.of(2020, 2, 29), LocalDate.of(2021, 2, 28))); expected resutl is 1.
      ACTUAL -
      For: System.out.println(MONTHS.between(LocalDate.of(2020, 2, 29), LocalDate.of(2021, 2, 28))); actual result is 11.
      For: System.out.println(YEARS.between(LocalDate.of(2020, 2, 29), LocalDate.of(2021, 2, 28))); actual resutl is 0.

      ---------- BEGIN SOURCE ----------
      System.out.println(MONTHS.between(LocalDate.of(2020, 2, 29), LocalDate.of(2021, 2, 28))); // prints out 11, not 12
      System.out.println(YEARS.between(LocalDate.of(2020, 2, 29), LocalDate.of(2021, 2, 28))); // prints out 0, not 1
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The only workaround is to add the additional check in the code if the first parameter of the method in last of February in a leap year and then add one day to the second parameter of the method.

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: