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

GregorianCalendar doesn't check invalid dates in non-lenient

XMLWordPrintable

    • b28
    • generic
    • generic

      GregorianCalendar validates the field values before resolving the calendar fields set by an application. As a result, it performs lenient operations in non-lenient mode. The following is a sample program.

      --
      import java.util.*;


      public class Feb29 {
          public static void main(String[] args) {
              GregorianCalendar gcal = new GregorianCalendar();
              gcal.clear();
              gcal.setLenient(false);
              //gcal.set(gcal.YEAR, 1970);
              gcal.set(gcal.MONTH, gcal.FEBRUARY);
              gcal.set(gcal.DAY_OF_MONTH, 29);
              Date date = gcal.getTime();
              System.out.println(date);
          }
      }

      ---
      The output:

      % java Feb29
      Sun Mar 01 00:00:00 JST 1970

      If the comment line is enabled, it throws an exception.

      % java Feb29
      Exception in thread "main" java.lang.IllegalArgumentException
              at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:1519)
              at java.util.Calendar.updateTime(Calendar.java:1552)
              at java.util.Calendar.getTimeInMillis(Calendar.java:912)
              at java.util.Calendar.getTime(Calendar.java:888)
              at Feb29.main(Feb29.java:12)

            okutsu Masayoshi Okutsu
            okutsu Masayoshi Okutsu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: