欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
How to Customize JAXB Bindings for Dates
 

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.   
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
GaussDB T 強體驗:通過(guò) DBeaver/RazorSQL/DbVisualizer工具連接數據庫(附測試賬號)
Jaxb 完全手冊
關(guān)于關(guān)系型數據庫差異
Java中的Date Time 與SQLServer 2005里的Datetime 之間的交互
java.sql.Date和java.sql.Timestamp轉換
日期在String和Date類(lèi)型轉換;ParsePosition,formatter.parse,java.sql.Timestamp
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久