Showing posts with label XMLGregorianCalendar. Show all posts
Showing posts with label XMLGregorianCalendar. Show all posts

Friday, June 05, 2009

JAXB2, XMLGregorianCalendar, JODA, JSR310

The variants of xs:date as defined in the XML Schema are mapped to XMLGregorianCalendar by JAXB2. XGC is not a very fun class to work with. JODA is an open source date time API implementation that is available in the open source world, which is the primary driver behind the JSR-310 specification.

Here is an email thread in JSR-310 that summarises the mapping of xs:date and JODA/JSR 310.

Email Link

Tuesday, December 09, 2008

XMLGregorianCalendar, DateTime etc

If you are interested in creating a new instance of XMLGregorianCalendar, then the following code should work for the current time and timezone.

==============================================
DatatypeFactory dtf = DatatypeFactory.newInstance();
XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar();

Calendar cal = Calendar.getInstance();
xgc.setYear(cal.get(Calendar.YEAR));
xgc.setDay(cal.get(Calendar.DAY_OF_MONTH));
xgc.setMonth(cal.get(Calendar.MONTH)+ 1);
xgc.setHour(cal.get(Calendar.HOUR_OF_DAY));
xgc.setMinute(cal.get(Calendar.MINUTE));
xgc.setSecond(cal.get(Calendar.SECOND));
xgc.setMillisecond(cal.get(Calendar.MILLISECOND));

// Calendar ZONE_OFFSET and DST_OFFSET fields are in milliseconds.
int offsetInMinutes = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000);
xgc.setTimezone(offsetInMinutes);
=================================================

Simpler option:

=================================================
GregorianCalendar gc = new GregorianCalendar();
DatatypeFactory dtf = DatatypeFactory.newInstance();
XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar(gc);
==================================================