I often need to work with dates in web services implementations, here is the JAXB custom binding lines I use to un/marshall JodaTime types to standard xs:date and xs:dateTime XML Schema types.
Note : I’m using the RI implementation of JAXB spec bundled in Metro.
DateTimeXmlAdapter that convert Joda-Time types to standard java types
import java.util.Date;import javax.xml.bind.annotation.XmlTransient;import javax.xml.bind.annotation.adapters.XmlAdapter;import org.joda.time.DateTime;@XmlTransientpublic class DateTimeXmlAdapter extends XmlAdapter {@Overridepublic DateTime unmarshal(Date date) throws Exception {return new DateTime(date.getTime());}@Overridepublic Date marshal(DateTime dateTime) throws Exception {return new Date(dateTime.getMillis());}}
JAXB Custom binder for binding xs:date to java.util.Date.
In the documentation, an example JAXB customization file that use this binder.
import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import javax.xml.bind.DatatypeConverter;/*** <?xml version="1.0" encoding="UTF-8"?>* <bindings xmlns="http://java.sun.com/xml/ns/jaxb"* version="2.0"* xmlns:xs="http://www.w3.org/2001/XMLSchema">* <globalBindings>* <javaType name="java.util.Date" xmlType="xs:date"* parseMethod="org.your.package.name.XSDateCustomBinder.parseDate"* printMethod="org.your.package.name.XSDateCustomBinder.printDate"* />* </globalBindings>* </bindings>*/public class XSDateCustomBinder {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);}}
JAXB Custom binder for binding xs:dateTime to java.util.Date.
In the documentation, an example JAXB customization file that use this binder.
import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import javax.xml.bind.DatatypeConverter;/*** <?xml version="1.0" encoding="UTF-8"?>* <bindings xmlns="http://java.sun.com/xml/ns/jaxb"* version="2.0"* xmlns:xs="http://www.w3.org/2001/XMLSchema">* <globalBindings>* <javaType name="java.util.Date" xmlType="xs:dateTime"* parseMethod="org.your.package.name.XSDateTimeCustomBinder.parseDateTime"* printMethod="org.your.package.name.XSDateTimeCustomBinder.printDateTime"* />* </globalBindings>* </bindings>*/public class XSDateTimeCustomBinder {public static Date parseDateTime(String s) {return DatatypeConverter.parseDate(s).getTime();}public static String printDateTime(Date dt) {Calendar cal = new GregorianCalendar();cal.setTime(dt);return DatatypeConverter.printDate(cal);}}
I hope this may help you.
Yes, your article is helpful indeed. It gave me a big jumpstart to do my own bindings for JAXB 2.0.5 fcs. Thanks.
I did run into a few problems and have overcame them, as listed below.
1. I had to insert another statement under the top-level , like this:
.
2. Jaxb/xjc generated for me a glue file: Adapter1.java for the bindings to work correctly:
public class Adapter1 extends XmlAdapter
And hey, thanks for introducing me to joda-time. Looks cool.
–Tom