How to Customize JAXB Bindings for Dates

来源:百度文库 编辑:神马文学网 时间:2024/04/27 18:12:27
http://eskatos.wordpress.com/2007/11/24/jaxb-custom-binding-for-joda-time/ 

I’m extensively using Joda-Time for handling time in my Java development, client or server side. Joda-Time is the codebase of the coming RI implementation of JSR-310 that will hopefully ship in Java 7.

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;/*** * *     *         *     * */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;/*** * *     *         *     * */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.

Written by eskatos

November 24, 2007 at 4:16 pm

Posted in Code

Tagged with java, jax-ws, jaxb, jee, metro

« Classpath tools : jcfind and JWhichCould it be dared to say that java is overdesigned ? »

5 Responses to 'JAXB Custom Binding for Joda-Time'

Subscribe to comments with RSS or TrackBack to 'JAXB Custom Binding for Joda-Time'.

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

Tom Truong

7 Dec 07 at 4:52 am

 

schema.xsd

view plaincopy to clipboardprint?
  1.  version="1.0" encoding="UTF-8"?>  
  2.  xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  3.    name="Root">  
  4.       
  5.        name="Element" type="xs:date"/>  
  6.       
  7.     
  8.   

schema.xjb

view plaincopy to clipboardprint?
  1.  version="1.0" encoding="UTF-8"?>  
  2.  version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" schemaLocation="schema.xsd">  
  3.     
  4.      name="java.util.Date" xmlType="xs:date" parseMethod="DateConverter.parseDate" printMethod="DateConverter.printDate" />  
  5.     
  6.   

DateConverter.java

view plaincopy to clipboardprint?
  1. import java.util.Calendar;   
  2. import java.util.Date;   
  3.   
  4. import javax.xml.bind.DatatypeConverter;   
  5.   
  6. public final class DateConverter {   
  7.   
  8.   public static Date parseDate(final String source) {   
  9.     return DatatypeConverter.parseDate(source).getTime();   
  10.   }   
  11.   
  12.   public static String printDate(final Date date) {   
  13.     final Calendar cal = Calendar.getInstance();   
  14.   
  15.     cal.setTime(date);   
  16.   
  17.     return DatatypeConverter.printDate(cal);   
  18.   }   
  19.   
  20.   private DateConverter() {   
  21.   }   
  22.