When building beans from a schema, JAXB defaults date and time types to Java's XMLGregorianCalendar.
If a project is using Joda Time, the desired default behaviour will almost certainly be to map:
xs:date to org.joda.time.LocalDate
xs:time to org.joda.time.LocalTime
xs:dateTime to org.joda.time.DateTime
The behaviour can be changed by first including the attached DatatypeConverter.java file into the joda-time build so that it's available to all users of Joda Time.
Then, the user can activate the conversion by adding the following binding (xjb) file:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="
<a href=" http:="" java.sun.com="" xml="" ns="" jaxb"="">http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
">
<jaxb:globalbindings>
<jaxb:javatype xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.LocalDate" xmltype="xs:date" parsemethod="org.joda.time.jaxb.DatatypeConverter.parseDate" printmethod="org.joda.time.jaxb.DatatypeConverter.printDate">
<jaxb:javatype xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.LocalTime" xmltype="xs:time" parsemethod="org.joda.time.jaxb.DatatypeConverter.parseTime" printmethod="org.joda.time.jaxb.DatatypeConverter.printTime">
<jaxb:javatype xmlns="http://java.sun.com/xml/ns/jaxb" name="org.joda.time.DateTime" xmltype="xs:dateTime" parsemethod="org.joda.time.jaxb.DatatypeConverter.parseTimestamp" printmethod="org.joda.time.jaxb.DatatypeConverter.printTimestamp">
</jaxb:javatype></jaxb:javatype></jaxb:javatype></jaxb:globalbindings>
</jaxb:bindings>
Joda-Time v2 has static parse methods on the classes themselves, so I assume this would work:
parseMethod= "org.joda.time.LocalTime.parse"
For the print, can it not call toString() directly?
printMethod= "org.joda.time.LocalTime.toString"
@scolebourne - good suggestion, thanks! One can't use toString() but there's a convenience method String.valueOf(Object) that just calls toString() on the object that you pass in which works nicely:
My only remaining concern is that the parse methods of the Joda Time classes don't handle all allowed strings as specified by the lexical datatype definition of xs:dateTime, xs:time and xs:date. However, my unit tests pass which mean that Joda Time is correctly parsing all the example XMLs that I use, so that's good enough for me at the moment. Also, the XSD looks pretty strict:
http://www.w3.org/TR/xmlschema-2/#dateTime
3.2.7.1 Lexical representation
The ·lexical space· of dateTime consists of finite-length sequences of characters of the form: '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?, where
So, I doubt I'll run into problems.
Closing as no action, and thanks for documenting this for future googling.
This request is invalid.
Closing as invalid seen as Joda Time already supports my use case well.