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

XMLGregorianCalendar.toGregorianCalendar methods do not set default values

XMLWordPrintable

    • 1.3
    • generic
    • generic

        Name: erR10175 Date: 04/12/2004



           Two methods in the class javax.xml.datatype.XMLGregorianCalendar

        public GregorianCalendar toGregorianCalendar()
        public GregorianCalendar toGregorianCalendar(TimeZone timezone, Locale aLocale, XMLGregorianCalendar defaults)

        do not set default values for the created GregorianCalendar objects if the corresponding
        XMLGregorianCalendar field is undefined.

        The javadoc of the first method reads:
        "
        When this instance has an undefined field, this conversion relies on the java.util.GregorianCalendar
        default for its corresponding field.
        "
        According to the javadoc the methods should set the year filed to 1970 if it is undefined
        for the XMLGregorianCalendar instance.

        The bug affects the following new JCK-15 beta2 tests:

          api/javax_xml/datatype/XMLGregorianCalendar/index.html#ConvertTo[ToGregorianCalendar006]
          api/javax_xml/datatype/XMLGregorianCalendar/index.html#ConvertTo[ToGregorianCalendar007]

        The bug is found in jdk1.5.0/beta2/b46. Those tests are in KFL with the RI bug

        5023932 XMLGregorianCalendar.toGregorianCalendar methods throw NPE if year is undefined

        To reproduce the bug compile and run the following code as shown in the log below.
        ------------------------------------------ test.java
        import javax.xml.datatype.XMLGregorianCalendar;
        import java.util.GregorianCalendar;

        class test {

            public static void main(String [] args) {
                XMLGregorianCalendar xmlCalendar
                    = XMLGregorianCalendar.parse("2000Z");
                xmlCalendar.setYear(XMLGregorianCalendar.FIELD_UNDEFINED);

                GregorianCalendar calendar = xmlCalendar.toGregorianCalendar();

                int year = calendar.get(GregorianCalendar.YEAR);
                int month = calendar.get(GregorianCalendar.MONTH);
                int day = calendar.get(GregorianCalendar.DAY_OF_MONTH);
                int hour = calendar.get(GregorianCalendar.HOUR);
                int minute = calendar.get(GregorianCalendar.MINUTE);
                int second = calendar.get(GregorianCalendar.SECOND);
                int millis = calendar.get(GregorianCalendar.MILLISECOND);

                if (year != 1970
                 || month != GregorianCalendar.JANUARY
                 || day != 1 || hour != 0 || minute != 0
                 || second != 0 || millis != 0) {
                    System.out.println("Failed: returned calendar values (year=" + year
                           + ", month=" + month + ", day_of_month=" + day
                           + ", hour=" + hour + ", minute=" + minute
                           + ", second=" + second
                           + ", millis=" + millis + "), but expected "
                           + "(year=1970, month=" + GregorianCalendar.JANUARY
                           + ", day_of_month=1, hour=0, minute=0, second=0, millis=0)");
                } else
                    System.out.println("OK");
            }
        }
        ----------------------------------------------------

        ------------------------------------------------ log
        $javac test.java && java -cp . -showversion test
        java version "1.5.0-beta2"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b46)
        Java HotSpot(TM) Server VM (build 1.5.0-beta2-b46, mixed mode)

        Failed: returned calendar values (year=172830195, month=1, day_of_month=5, hour=2, minute=6, second=28, millis=352), but expected (year=1970, month=0, day_of_month=1, hour=0, minute=0, second=0, millis=0)
        ----------------------------------------------------

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

              jsuttorsunw Jeff Suttor (Inactive)
              reysunw Rey Rey (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: