Skip to content

Instantly share code, notes, and snippets.

@musubu
Created April 29, 2012 19:53
Show Gist options
  • Select an option

  • Save musubu/2552948 to your computer and use it in GitHub Desktop.

Select an option

Save musubu/2552948 to your computer and use it in GitHub Desktop.
XMLGregorianCalendarConversionutil.java
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class XMLGregorianCalendarConversionUtil {
private static DatatypeFactory datatypeFactory = null;
static {
try {
datatypeFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
throw new IllegalStateException("Error while trying to obtain a new instance of DatatypeFactory", e);
}
}
public static XMLGregorianCalendar asXMLGregorianCalendar(Date date) {
if (date == null) {
return null;
} else {
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTimeInMillis(date.getTime());
gregorianCalendar.setTimeZone(TimeZone.getTimeZone("UTC"));
return datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
}
}
public static Date asDate(XMLGregorianCalendar xmlGregorianCalendar) {
if (xmlGregorianCalendar == null) {
return null;
} else {
return xmlGregorianCalendar.toGregorianCalendar().getTime();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment