-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
None
-
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
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
- relates to
-
JDK-4254589 Doc: GregorianCalendar needs to specify behavior for leap year add/roll
- Resolved
-
JDK-8327007 javax/swing/JSpinner/8008657/bug8008657.java fails
- Resolved
-
JDK-8327167 Clarify the handling of Leap year by Calendar
- Resolved
-
JDK-8331646 Add specific regression leap year tests
- Resolved