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

Calendar.add() is not round-trip safe on a Leap Day

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None

      Consider this code which subtracts a year then adds it back.
      If the original date is a leap day, then it is not round-trip safe

      import java.util.Calendar;

      public class LeapDayBug {
         public static void main(String[] args) {
              Calendar calendar = Calendar.getInstance();
              calendar.set(2024, 1, 29);
              System.out.println(calendar.getTime());
              calendar.add(Calendar.YEAR, -1);
              calendar.add(Calendar.YEAR, 1);
              System.out.println(calendar.getTime());
         }
      }

      % java LeapDayBug.java
      Thu Feb 29 12:27:46 PST 2024
      Wed Feb 28 12:27:46 PST 2024

            naoto Naoto Sato
            prr Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: