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

GregorianCalendar.add(int field, int amount) works wrong on Feb-29

XMLWordPrintable

    • 1.1.8
    • sparc
    • solaris_2.5
    • Verified

        Name: dfC67450 Date: 09/17/98

        If we add 11 months to Feb-29-1996 we should obtain Jan-29-1997 but
        we obtain Jan-28-1997.

        Here is the test demonstrating the bug:
        ------------- Test.java -----------
        import java.util.*;

        public class Test {
            public static void main (String args[]){
              GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29);
              System.out.println("init date: " + calendar.getTime());
              System.out.println();

              calendar.add(Calendar.MONTH, 10);
              Date date1 = calendar.getTime();
              int d1 = calendar.get(Calendar.DAY_OF_MONTH);
              System.out.println("after adding 10 months: " + date1);

              calendar = new GregorianCalendar(1996, 1, 29);
              calendar.add(Calendar.MONTH, 11);
              Date date2 = calendar.getTime();
              int d2 = calendar.get(Calendar.DAY_OF_MONTH);
              System.out.println("after adding 11 months: " + date2);

              if (d1 == d2) {
                System.out.println("Test passed");
              } else {
                System.out.println("Test failed");
              }
            }
        }
        ---------Output from the test---------------------
        init date: Thu Feb 29 00:00:00 GMT+03:00 1996

        after adding 10 months: Sun Dec 29 00:00:00 GMT+03:00 1996
        after adding 11 months: Tue Jan 28 00:00:00 GMT+03:00 1997
        Test failed
        -------------------------------------------------

        This bug arised after bug 4165343 has been fixed.

        ======================================================================

              aliusunw Alan Liu (Inactive)
              dfazunensunw Dmitri Fazunenko (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: