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);
    }

}

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

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

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

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

    INSTANCE;

    private DatatypeFactory factory;

    private DateUtils() {
        try {
            factory = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException dtce) {
            throw new RuntimeException(dtce);
        }
    }

    public Date parseIso8601(String date) throws ParseException {
        XMLGregorianCalendar cal = factory.newXMLGregorianCalendar(date);
        return cal.toGregorianCalendar().getTime();
    }

    public String formatIso8601(Date date, TimeZone zone) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        if (zone != null) {
            cal.setTimeZone(zone);
        }
        XMLGregorianCalendar calXml = factory.newXMLGregorianCalendar(cal);
        if (zone == null) {
            // display as UTC
            calXml = calXml.normalize();
        }
        return calXml.toXMLFormat();
    }

    public String formatIso8601(Date date) {
        return formatIso8601(date, null);
    }

}

Use XMLCalendar to handle iso 8601 dates
Use XMLCalendar to handle iso 8601 dates