From f39db8937c9b91a69ad9622fa254bdfbebc82fa5 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 31 May 2015 13:10:13 +0200 Subject: [PATCH] Custom xs:dateTime binder --- .../binders/XSDateTimeCustomBinder.java | 30 ++++++++++++++++++++++ .../src/main/resources/bindings/custombinding.xjb | 9 ++++++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java diff --git a/web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java b/web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java new file mode 100644 index 0000000..46076f4 --- /dev/null +++ b/web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java @@ -0,0 +1,30 @@ +package de.spring.webservices.binders; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + + +public class XSDateTimeCustomBinder extends XmlAdapter { + + @Override + public Date unmarshal(final String dateTime) throws Exception { + // X pattern just works from Java >= 1.7 + final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + formatter.setTimeZone(TimeZone.getTimeZone("Europe/Madrid")); + + return formatter.parse(dateTime); + } + + @Override + public String marshal(final Date dateTime) throws Exception { + // X pattern just works from Java >= 1.7 + final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); + formatter.setTimeZone(TimeZone.getTimeZone("Europe/Madrid")); + + return formatter.format(dateTime); + } +} diff --git a/web-services-spring-server/src/main/resources/bindings/custombinding.xjb b/web-services-spring-server/src/main/resources/bindings/custombinding.xjb index eea1cfe..726784a 100644 --- a/web-services-spring-server/src/main/resources/bindings/custombinding.xjb +++ b/web-services-spring-server/src/main/resources/bindings/custombinding.xjb @@ -4,8 +4,9 @@ xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" xmlns:annox="http://annox.dev.java.net" + xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.1" - jaxb:extensionBindingPrefixes="inheritance annox"> + jaxb:extensionBindingPrefixes="xjc inheritance annox"> + + + + + -- 2.1.4