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

Leap yr. Dec. 31st, setLenient(false): can only add 4-yr multiples to YEAR fld

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.2.2, 1.3.0, 1.4.0
    • core-libs
    • generic, x86
    • generic, windows_nt



      Name: krC82822 Date: 12/16/2000


      orig synopsis: "Calendar bug When 2 years are added to Dec 31st of a leap year"

      16 Dec 2000, eval1127@eng -- reproducible with 1.3.0 and 1.4 beta45 (see below)
      [filed against: Classic VM (build JDK-1.2.2-W, native threads, nojit)]
      However, user's claim that adding 3 years works OK is incorrect. Adding anything
      other than multiples of 4 years (i.e., choosing anything other than another
      leap year) does not appear to work. See also #'s 4266783, 4147269.
      ----------------------------
      This problem occurs ONLY WHEN the following is true - for the Calendar object
      setLenient(false) is used,
      the year is set to a leap year,
      the date is set to December 31st of the leap year, and
      "2" years are added to the year field.

      Adding 3 [THIS IS NOT TRUE], 4 ... years does NOT throw this exception,
      Any other date in the year does NOT throw this exception, Adding 2 years
      to Dec 31st of any other year does NOT throw this exception either.

      Here is the code

      import java.util.Calendar;
      import java.util.GregorianCalendar;

      public class TestDates {
          private static void main(String[] _args){
              // Does not work
              GregorianCalendar c = new GregorianCalendar();
              c.setLenient(false);
              c.set(2000, 11, 31);
              System.out.println(c.getTime());
              c.add(Calendar.YEAR, 2);
              System.out.println(c.get(Calendar.YEAR) + "/" + c.get(Calendar.MONTH)
      + "/" + c.get(Calendar.DATE));
          }
      }

      Here is the runtime exception;

      Exception in thread "main" java.lang.IllegalArgumentException
              at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:1334)
              at java.util.Calendar.updateTime(Calendar.java:1311)
              at java.util.Calendar.complete(Calendar.java:874)
              at java.util.Calendar.get(Calendar.java:734)
              at TestDates.main(TestDates.java:14)

      -------------

      % java -version
      java version "1.4.0beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b45)
      Java HotSpot(TM) Client VM (build 1.4beta-B45, mixed mode)
      % cd
      % cd tests
      % javac -d . TestDates113829.java
      % java TestDates113829
      Sun Dec 31 09:31:39 PST 2000
      Exception in thread "main" java.lang.IllegalArgumentException
              at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:1438)
              at java.util.Calendar.updateTime(Calendar.java:1533)
              at java.util.Calendar.complete(Calendar.java:1083)
              at java.util.Calendar.get(Calendar.java:941)
              at TestDates113829.main(TestDates113829.java:28)



      (Review ID: 113829)
      ======================================================================

      Name: rmT116609 Date: 12/19/2000


      java version "1.2.2"
      Classic VM (Build JDK-1.2.2-001, native threads, symcjit)

      while using Calendar to do some math on dates, I encountered a problem. When I
      try to do some math on Calendar object having value 31 dec for any leap year,
      the resultant Calendar object becomes invalid. Here's the examples I tried

      // Does not work
      java.util.Calendar c = java.util.Calendar.getInstance();
      c.clear();
      c.setLenient(false);
      c.set(1904, 11, 31);
      System.out.println(c.getTime());
      c.add(java.util.Calendar.YEAR, 2);
      System.out.println(c.getTime());

      // Works
      java.util.Calendar c = java.util.Calendar.getInstance();
      c.clear();
      c.setLenient(false);
      c.set(1900, 11, 31);
      System.out.println(c.getTime());
      add(java.util.Calendar.YEAR, 2);
      System.out.println(c.getTime());

      The error message I get is-

      java.lang.IllegalArgumentException
      at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:1368)
      at java.util.Calendar.updateTime(Calendar.java:1508)
      at java.util.Calendar.getTimeInMillis(Calendar.java:890)
      at java.util.Calendar.before(Calendar.java:1113)
      at gov.hud.hud50058.VLFamily.CheckMI(VLFamily.java)
      at gov.hud.hud50058.VLFamily.checkFamily(VLFamily.java)
      at gov.hud.hud50058.Validator.validate(Validator.java)
      at gov.hud.hud50058.Validator.validateAll(Validator.java)
      at gov.hud.hud50058.Converter.convertToXML(Converter.java)
      at gov.hud.hud50058.Scheduler.run(Scheduler.java)
      (Review ID: 113818)
      ======================================================================

            peytoia Yuka Kamiya (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: