Created
April 29, 2012 19:53
-
-
Save musubu/2552948 to your computer and use it in GitHub Desktop.
XMLGregorianCalendarConversionutil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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