Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import javax.xml.datatype.DatatypeConstants; import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.datatype.DatatypeFactory; public class Main { /** * Convenience method to parse an XML Date Time into a Date. Only useful * when the XML Date Time is within the Date object time range. * * @param toParse * the xml date time string to parse. * @return the parsed Date object. */ public static Date getDate(final String toParse) { XMLGregorianCalendar calendar = getXMLGregorianCalendar(toParse); if (calendar != null) { return new Date(calendar.toGregorianCalendar().getTimeInMillis()); } else { return null; } } /** * Converts an XMLGregorianCalendar to a Date. * * @param xmlDate * XMLGregorianCalendar to convert. * @return corresponding date object. */ public static Date getDate(final XMLGregorianCalendar xmlDate) { // TODO: is this equivalent to getDate(String) processing above?? // start with UTC, i.e. no daylight savings time. TimeZone timezone = TimeZone.getTimeZone("GMT"); // adjust timezone to match xmldate int offsetMinutes = xmlDate.getTimezone(); if (offsetMinutes != DatatypeConstants.FIELD_UNDEFINED) { timezone.setRawOffset( // convert minutes to milliseconds offsetMinutes * 60 // seconds per minute * 1000 // milliseconds per second ); } // use calendar so parsed date will be UTC Calendar calendar = Calendar.getInstance(timezone); calendar.clear(); calendar.set(xmlDate.getYear(), // xmlcalendar is 1 based, calender is 0 based xmlDate.getMonth() - 1, xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(), xmlDate.getSecond()); Date date = calendar.getTime(); int millis = xmlDate.getMillisecond(); if (millis != DatatypeConstants.FIELD_UNDEFINED) { calendar.setTimeInMillis(calendar.getTimeInMillis() + millis); } return date; } /** * Parse an XML Date Time into an XMLGregorianCalendar. * * @param toParse * the xml date time string to parse. * @return the parsed XMLGregorianCalendar object. */ public static XMLGregorianCalendar getXMLGregorianCalendar(final String toParse) { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(toParse); } catch (Exception e) { return null; } } }