Custom xs:dateTime binder
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 31 May 2015 11:10:13 +0000 (13:10 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 31 May 2015 11:10:13 +0000 (13:10 +0200)
web-services-spring-server/src/main/java/de/spring/webservices/binders/XSDateTimeCustomBinder.java [new file with mode: 0644]
web-services-spring-server/src/main/resources/bindings/custombinding.xjb

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 (file)
index 0000000..46076f4
--- /dev/null
@@ -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<String, Date> {
+
+       @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);
+    }
+}
index eea1cfe..726784a 100644 (file)
@@ -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">
     
     <!--
     Se procesa con Xpath si quieres meter expresiones regulares y cosas así en teoría dependes de Xpath
     <!-- The same applies to annotate. If you do not want or you may not modify your xsd files you can
          modify instead this file with your custom binding :) -->
 
+    <!-- Custom xs:dateTime adapter -->
+    <jaxb:globalBindings>
+       <xjc:javaType adapter="de.spring.webservices.binders.XSDateTimeCustomBinder"
+            name="java.util.Date" xmlType="xs:dateTime" />
+    </jaxb:globalBindings>
+
 </jaxb:bindings>