打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How to Customize JAXB Bindings for Dates
userphoto

2009.03.31

关注
 

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

Written by eskatos

November 24, 2007 at 4:16 pm

Posted in Code

Tagged with , , , ,

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

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  3.   <xs:complexType name="Root">  
  4.     <xs:sequence>  
  5.       <xs:element name="Element" type="xs:date"/>  
  6.     </xs:sequence>  
  7.   </xs:complexType>  
  8. </xs:schema>  

schema.xjb

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

DateConverter.java

  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.   
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
GaussDB T 强体验:通过 DBeaver/RazorSQL/DbVisualizer工具连接数据库(附测试账号)
Jaxb 完全手册
关于关系型数据库差异
Java中的Date Time 与SQLServer 2005里的Datetime 之间的交互
java.sql.Date和java.sql.Timestamp转换
日期在String和Date类型转换;ParsePosition,formatter.parse,java.sql.Timestamp
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服