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

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: None
    • Component/s: 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

            Assignee:
            Naoto Sato
            Reporter:
            Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: