Skip to main content

How do I map xs:date to java.util.Date?

Posted by kohsuke on March 22, 2006 at 3:34 PM PST

santacruz40 wanted JAXB 2.0 XJC to map xs:date to java.util.Date. Here's how to do it.

The easiest way to do this is to simply modify the generated code. Just find out all the references to XMLGregorianCalendar and replace them with Date. With a modern IDE, this is surprisingly easy. But this only works if your schema doesn't change too often, for you don't want to do this too frequently.

If you'd rather have XJC generate the right thing for you, then you need to writea customization:

<br /><xs:schema elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" targetNamespace="calendar-schemalet"><br />  <xs:annotation><xs:appinfo><br />    <jaxb:globalBindings><br />      <jaxb:javaType name="java.util.Calendar" xmlType="xs:date"<br />        parseMethod="javax.xml.bind.DatatypeConverter.parseDate"<br />        printMethod="javax.xml.bind.DatatypeConverter.printDate"<br />        /><br />    </jaxb:globalBindings><br />  </xs:appinfo></xs:annotation><br /></xs:schema><br />

The javaType customization takes care of this. You can either copy the portion and paste that into your schema, or you can save this whole schema as calendar.schemalet and pass it to XJC along with your other schemas.

Now, this maps xs:date to Calendar. To map this to Date, you need to define a pair static methods that convert from/to XML string and Date. Fortunately, that can be done relatively easily by using the above parse/printDate functions:

public class DateAdapter {
  public static Date parseDate(String s) {
    return DatatypeConverter.parseDate(s).getTime();
  }
  public static String printDate(Date dt) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(dt);
    return DatatypeConverter.printDate(cal);
  }
}

Then replace the parse/printMethod attributes of the javaType customization with these methods.

Comments

I ran into the mapping problem when marshaling recently and ...

I ran into the mapping problem when marshaling recently and I immediately began to think I needed to add code to do the translation. All google results indicated I had to do more work. However, I didn't notice that the XMLGregorianCalendar had a getXMLSchemaType method. This lead me to believe that it was aware of what type it was translating from to. And in fact it is, this may have been the only solution in the past but if you are seeing XMLGregorianCalendar having problems you can just set certain columns of it to get the time you need. For example: xs:date is year, month and day only. If you're experiencing this problem in 2012, look at the documentation for the class. It helps.

Problem with XMLGregorianCalendar field in SOA request

We have a SOA service operation which has an request attribute of type XMLGregorianCalendar. In the WSDL file, attribute is defined as xs:date. On the SOA client side request object has correct date and time values(eg 2008-05-12T05:12:00). But in the implementation side the request object has the year,month and day fields populated correctly, where as the time(hour,minute and second) never gets set. On exploring the request object on the service implementation side the hour, minute and second always have the value as -2147483648 Instead of java client code, if we use SOAP ui to construct request and invoke the service implementation, both date and time gets populated correctly in the client code, till the control reaches the proxy class timestamp values is present in the request. But when the xml request is getting constructed(which we have logged in txt file by modifying clientconfig.xml) the date value is present but time value is getting dropped We had actually written the WSDL and had used axis2 eclipse plugin to generate request/response objects ,service & client stubs from it As our WSDL is already in production, please let us know if we have some sort of work around, to modify the Webservice client code without going for WSDL change. It is also not possible to tamper the request objects as they are generated from WSDL and are packaged as jar file and placed in production If in XML request, we are able to construct the time stamp, the service (generated from WSDL) is able to capture date time values. This was evident by constructing the timestamp request via SOAP UI / Lisa tool This workaround fix is required for time being which is very critical.. Thanks in advance.