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

LocalDate minusDays incorrect after the first year rollover

XMLWordPrintable

      FULL PRODUCT VERSION :
      Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
      java version "1.8.0_162-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_162-ea-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 25.162-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      OSX 10.11.6

      A DESCRIPTION OF THE PROBLEM :
      LocalDate.minusDays() does not decrement the year counter correctly after the first rolled over year.

      this can be illustrated using a for loop and subtracting days. The 2017-2016 year boundary decrements correctly, but the subsequent ones do not. This is an excerpt of the 2016-2015 boundary:
      01/5/2016
      01/4/2016
      01/3/2016
      01/2/2016
      01/1/2016
      12/31/2016
      12/30/2016
      12/29/2016
      12/28/2016
      12/27/2016
      12/26/2015
      12/25/2015
      12/24/2015
      12/23/2015
      12/22/2015

      Notice how the 2016 year value extends into the last days of December, even though it should be December 2015.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.time.LocalDate;
      import java.time.ZoneId;
      import java.time.format.DateTimeFormatter;

      public class DateTimeDebug {

          public static void main(String[] args) {

             // also LocalDate.now(); has the same bug
              LocalDate today = LocalDate.of(2017, 12, 26);

              // subtract 2 years
              System.out.println("2017/12/26 minus 730 days should be: 2015/12/26, but was: " + today.minusDays(730).format(DateTimeFormatter.ofPattern("MM/d/YYYY")));
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      2017/12/26 minus 730 days should be: 2015/12/26, but was: 12/27/2016
      ACTUAL -
      2017/12/26 minus 730 days should be: 2015/12/26, but was: 12/27/2016

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.time.LocalDate;
      import java.time.ZoneId;
      import java.time.format.DateTimeFormatter;

      public class DateTimeDebug {

          public static void main(String[] args) {

             // also LocalDate.now(); has the same bug
              LocalDate today = LocalDate.of(2017, 12, 26);

              // subtract 2 years
              System.out.println("2017/12/26 minus 730 days should be: 2015/12/26, but was: " + today.minusDays(730).format(DateTimeFormatter.ofPattern("MM/d/YYYY")));
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      minusYears also broken.

      it seems like an edge case with the start date (when "now" is), as this was working fine in early november, and previous months.

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: