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

XMLGregorianCalendar toXXX and fromXXX methods use broken format

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b35
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Description

      Name: erR10175 Date: 11/18/2003


       
        The following methods of the class javax.xml.datatype.XMLGregorianCalendar
        
      public String toDateType()
      public String toGMonthDayType()
      public String toGDayType()
      public static XMLGregorianCalendar fromDateType(String lexicalRepresentation)
      public static XMLGregorianCalendar fromGDayType(String lexicalRepresentation)
      public static XMLGregorianCalendar fromGMonthDayType(String lexicalRepresentation)

      do not work correctly: the toXXX methods return invalid strings (see the test below),
      the fromXXX methods throw IllegalArgumentException for valid values.

        Another problem with the following methods:

      public String toDateType()
      public String toGYearMonthType()
      public String toGMonthType()
      public String toGMonthDayType()
      public String toDateTimeType()
      public static XMLGregorianCalendar fromDateType(String lexicalRepresentation)
      public static XMLGregorianCalendar fromDateTimeType(String lexicalRepresentation)
      public static XMLGregorianCalendar fromGMonthDayType(String lexicalRepresentation)
      public static XMLGregorianCalendar fromGMonthType(String lexicalRepresentation)

      the toXXX methods convert the month field directly from int to its string
      representation. As shown in the test below, Calendar.JANUARY is converted to '00'.
      Actually, it should be incremented first with 1 to let Calendar.JANUARY (which is 0)
      be mapped to '01' as the XML Schema spec specifies (see XML Schema Part 2: Datatypes,
      Section 3.2.9 date). The same is with fromXXX methods.

      This bug affects new test in JCK 1.5 (not integrated yet)
         api/javax_xml/datatype/XMLGregorianCalendar/index.html#XMLGregorianCalendar[ToDateType001]

      The bug is found in jdk1.5.0/beta/b28.

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

      class test {
          public static void main(String [] args) {
              XMLGregorianCalendar calendar // January 26th, 1999
                  = new XMLGregorianCalendar(1999,XMLGregorianCalendar.JANUARY,26);
              calendar.unsetTimeZone();

              System.out.println("converting 1999-01-26 to string ...");
              String returned = calendar.toDateType();
              if (!returned.startsWith("1999-01-26")) {
                  System.out.println("Failed: returned: " + returned
                                   + ", expected 1999-01-26");
              } else {
                  System.out.println("OK");
              }

              System.out.println("parsing 1999-01-26 ...");
              calendar = XMLGregorianCalendar.fromDateType("1999-01-26");
              int year = calendar.get(XMLGregorianCalendar.YEAR);
              int month = calendar.get(XMLGregorianCalendar.MONTH)
                        + 1 - XMLGregorianCalendar.JANUARY;
              int day = calendar.get(XMLGregorianCalendar.DAY_OF_MONTH);
              if (day != 26 || month != 1 || year != 1999) {
                  System.out.println("Failed: returned: " + year
                                   + "-" + month + "-" + day
                                   + ", expected 1999-01-26");
              } else {
                  System.out.println("OK");
              }
          }
      }
      ----------------------------------------------------

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

      converting 1999-01-26 to string ...
      Failed: returned: 1999-00-03/11/12z, expected 1999-01-26
      parsing --08-- ...
      Failed: returned: 9th month, expected: 8th
      parsing 1999-01-26 ...
      Exception in thread "main" java.lang.IllegalArgumentException: 1999-01-26
              at javax.xml.datatype.XMLGregorianCalendar$Parser.skip(XMLGregorianCalendar.java:888)
              at javax.xml.datatype.XMLGregorianCalendar$Parser.parse(XMLGregorianCalendar.java:807)
              at javax.xml.datatype.XMLGregorianCalendar.<init>(XMLGregorianCalendar.java:279)
              at javax.xml.datatype.XMLGregorianCalendar.fromDateType(XMLGregorianCalendar.java:490)
              at test.main(test.java:30)
      ----------------------------------------------------

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

        Attachments

          Activity

            People

            Assignee:
            jfialli Joe Fialli
            Reporter:
            reysunw Rey Rey (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: