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

java.util.GregorianCalendar.computeTime() works wrong when lenient is false

    XMLWordPrintable

Details

    Description



      Name: dfC67450 Date: 06/10/98



      java.util.GregorianCalendar.computeTime() does not throw IllegalArgumentException
      when lenient is set to false and some fields have been set to illegal value.
      The list of fields the method fails with is: WEEK_OF_YEAR, WEEK_OF_MONTH, DAY_OF_YEAR,
      DAY_OF_WEEK_IN_MONTH.

      Javadoc says about GregorianCalendar.computeTime:
          
           Converts time field values to UTC as milliseconds.
           exception: IllegalArgumentException if any fields are invalid.
           
      Javadoc says about Calendar.setLenient:

           Specify whether or not date/time interpretation is to be lenient. With
           lenient interpretation, a date such as "February 942, 1996" will be
           treated as being equivalent to the 941st day after February 1, 1996.
           With strict interpretation, such dates will cause an exception to be
           thrown.

      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.util.*;

      public class Test {
        public static void main (String args[]){


          boolean passed = true;
          GregorianCalendarTest calendar = new GregorianCalendarTest();
          calendar.setLenient(false);
          Date date = (new GregorianCalendar(1996,0,3)).getTime();
          for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
            calendar.setTime(date);
            int max = calendar.getActualMaximum(field);
            int value = max+1;
            calendar.set(field, value);
            try {
                calendar.computeTime(); // call method under test
                System.out.println("Test failed with field " + fieldName[field]);
                System.out.println(" date before: " + date);
                System.out.println(" date after: " + calendar.getTime());
                System.out.println(" value: " + value + " (max = " + max +")");
                passed = false;
             } catch (IllegalArgumentException e) {
             }
           }

          if (passed) System.out.println("Test passed");
        }

        static final String[] fieldName = {
                "ERA",
                "YEAR",
                "MONTH",
                "WEEK_OF_YEAR",
                "WEEK_OF_MONTH",
                "DAY_OF_MONTH",
                "DAY_OF_YEAR",
                "DAY_OF_WEEK",
                "DAY_OF_WEEK_IN_MONTH",
                "AM_PM",
                "HOUR",
                "HOUR_OF_DAY",
                "MINUTE",
                "SECOND",
                "MILLISECOND",
                "ZONE_OFFSET",
                "DST_OFFSET"
        };


      }

      class GregorianCalendarTest extends GregorianCalendar {
               // access to protected method

          public GregorianCalendarTest(int year, int month, int day) {
            super(year, month, day);
          }
          public GregorianCalendarTest() {
            super();
          }
          public void computeTime() {
             super.computeTime();
          }

      }
      ---------Output from the test---------------------
      Test failed with field WEEK_OF_YEAR
        date before: Wed Jan 03 00:00:00 GMT+03:00 1996
        date after: Wed Jan 01 00:00:00 GMT+03:00 1997
        value: 53 (max = 52)
      Test failed with field WEEK_OF_MONTH
        date before: Wed Jan 03 00:00:00 GMT+03:00 1996
        date after: Wed Feb 07 00:00:00 GMT+03:00 1996
        value: 6 (max = 5)
      Test failed with field DAY_OF_YEAR
        date before: Wed Jan 03 00:00:00 GMT+03:00 1996
        date after: Wed Jan 01 00:00:00 GMT+03:00 1997
        value: 367 (max = 366)
      Test failed with field DAY_OF_WEEK_IN_MONTH
        date before: Wed Jan 03 00:00:00 GMT+03:00 1996
        date after: Wed Feb 07 00:00:00 GMT+03:00 1996
        value: 6 (max = 5)
      -------------------------------------------------

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

      Attachments

        Issue Links

          Activity

            People

              okutsu Masayoshi Okutsu
              dfazunensunw Dmitri Fazunenko (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: