United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4955787 : XMLGregorianCalendar toXXX and fromXXX methods use broken format

Details
Type:
Bug
Submit Date:
2003-11-18
Status:
Closed
Updated Date:
2012-04-25
Project Name:
JDK
Resolved Date:
2004-01-12
Component:
xml
OS:
generic
Sub-Component:
jaxp
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0 (b35)

Related Reports

Sub Tasks

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)
----------------------------------------------------

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

                                    

Comments
EVALUATION

We are planning a complete rewrite (including signature changes) of this class.
So the bug is expected to be removed as a part of it.
###@###.### 2003-11-19
                                     
2003-11-19
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger-beta

FIXED IN:
tiger-beta

INTEGRATED IN:
tiger-b35
tiger-beta

VERIFIED IN:
tiger-beta2


                                     
2004-06-14



Hardware and Software, Engineered to Work Together