Steps (method) to convert Calender to XMLGregorianCalendar:
There may be several ways to do the conversion as many methods are available in Calendar and other classes. Here is one of it:
- This may be required while working with jax-ws or jaxb.
- jax-ws uses jaxb internally.
public static Calendar fromXMLGregorianCalendar(XMLGregorianCalendar xc)
throws DatatypeConfigurationException {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(xc.toGregorianCalendar().getTimeInMillis());
return c;
}
public static XMLGregorianCalendar toXMLGregorianCalendar(Calendar c)
throws DatatypeConfigurationException {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(c.getTimeInMillis());
XMLGregorianCalendar xc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
return xc;
}
Notes:
XMLGregorianCalendar can be instantiated with:
XMLGregorianCalendar xc = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xc would be initialized with date: Thu Jan 01 00:00:00 GMT+05:30 1970
Note: If you directly try to print xc with: sysout(xc) then u will get Exception.
So you can use:
System.out.println(xc.toGregorianCalendar().getTime());
Output: Thu Jan 01 00:00:00 GMT+05:30 1970
I hope this would be useful.
Regards,
Ravindra Pandya
Thanks :)
ReplyDelete