package com.github.aesteve.vertx.nubes.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Singleton pattern implementation using enums.
 * 
 * See "Effective Java" by Joshua Bloch
 * 
 * @author aesteve
 */
public enum DateUtils {

    INSTANCE;

    private SimpleDateFormat iso8601Parser;

    private DateUtils() {
        // FIXME : use Java 8 API instead (with moments)
        iso8601Parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        iso8601Parser.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    public Date parseIso8601(String date) throws ParseException {
        return iso8601Parser.parse(date);
    }

    public String formatIso8601(Date date) {
        return iso8601Parser.format(date);
    }

}

@@ -89,79 +89,230 @@
 ava.
-text.SimpleDateFormat;%0Aimport java.util.Date;%0Aimport java.util.TimeZone
+util.Date;%0Aimport java.util.GregorianCalendar;%0Aimport java.util.TimeZone;%0A%0Aimport javax.xml.datatype.DatatypeConfigurationException;%0Aimport javax.xml.datatype.DatatypeFactory;%0Aimport javax.xml.datatype.XMLGregorianCalendar
 ;%0A%0A/
@@ -490,38 +490,31 @@
 ate 
-SimpleDateFormat iso8601Parser
+DatatypeFactory factory
 ;%0A%0A 
@@ -550,179 +550,574 @@
     
-// FIXME : use Java 8 API instead (with moments)%0A        iso8601Parser = new SimpleDateFormat(%22yyyy-MM-dd'T'HH:mm:ss.SSS'Z'%22
+try %7B%0A            factory = DatatypeFactory.newInstance();%0A        %7D catch (DatatypeConfigurationException dtce) %7B%0A            throw new RuntimeException(dtce);%0A        %7D%0A    %7D%0A%0A    public Date parseIso8601(String date) throws ParseException %7B%0A        XMLGregorianCalendar cal = factory.newXMLGregorianCalendar(date);%0A        return cal.toGregorianCalendar().getTime(
 );%0A    
+%7D%0A%0A
     
-iso8601Parser.setTimeZone(TimeZone.g
+public String formatIso8601(Date date, TimeZone zone) %7B%0A        GregorianCalendar cal = new GregorianCalendar();%0A        cal.setTime(date);%0A        if (zone != null) %7B%0A            cal.s
 etTi
@@ -1127,129 +1127,244 @@
 one(
-%22UTC%22)
+zone
 );%0A    
-%7D%0A%0A    public Date parseIso8601(String date) throws ParseException %7B%0A        return iso8601Parser.parse(date
+    %7D%0A        XMLGregorianCalendar calXml = factory.newXMLGregorianCalendar(cal);%0A        if (zone == null) %7B%0A            // display as UTC%0A            calXml = calXml.normalize();%0A        %7D%0A        return calXml.toXMLFormat(
 );%0A 
@@ -1433,33 +1433,32 @@
 urn 
-iso8601Parser.format(date
+formatIso8601(date, null
 );%0A 

Use XMLCalendar to handle iso 8601 dates