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

LocalDate.plusDays(1) does not switch year correctly from december to january

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • 8
    • core-libs
    • x86
    • windows_8

      FULL PRODUCT VERSION :
      jdk8u5

      ADDITIONAL OS VERSION INFORMATION :
      windows 8.1

      A DESCRIPTION OF THE PROBLEM :
      java.time.LocalDate.plusDays(1) does not always switch year correctly at end of december an begin of january. Seems to happen only in last/first week of year (less than 7 wrong dates).

      same behaviour when using java.time.LocalDate.minusDays(1).

      see Code in 'Steps to Reproduce'

      This is a show-stopper.

      For monthly and weekly presentation i tried to generate a List of LocalDate. I found no workaround with java.time API.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      java.time.LocalDate date = LocalDate.of(2014, Month.DECEMBER, 28); // 2014-12-28
      date = date.plusDays(1); // 2015-12-29 => expected 2014-12-29
      date = date.plusDays(1); // 2015-12-30 => expected 2014-12-30
      date = date.plusDays(1); // 2015-12-31 => expected 2014-12-31
      date = date.plusDays(1); // 2015-01-01


      LocalDate date = LocalDate.of(2020, Month.DECEMBER, 31); // 2020-12-31
      date = date.plusDays(1); // 2020-01-01 => expected 2021-01-01
      date = date.plusDays(1); // 2020-01-02 => expected 2021-01-02
      date = date.plusDays(1); // 2020-01-03 => expected 2021-01-03
      date = date.plusDays(1); // 2021-01-04

      // okay for year 2017
      LocalDate date = LocalDate.of(2017, Month.DECEMBER, 31); // 2017-12-31
      date = date.plusDays(1); // 2018-01-01




      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
          @Test
          public void testGetLastDaysOfDecember() {
              System.out.println("testGetLastDaysOfDecember()");
              DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd EEEE");
              for (int year = 2000; year < 2021; year++) {
                  System.out.println("\nYear " + year);
                  LocalDate date = LocalDate.of(year, Month.DECEMBER, 27);
                  for (int index = 0; index < 10; index++) {
                      System.out.println(" " + formatter.format(date));
                      date = date.plusDays(1);
                  }
              }
          }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      found none

            sherman Xueming Shen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: