How to convert java.time.ZonedDateTime to XMLGregorianCalendar?

Mikhail Boyarsky picture Mikhail Boyarsky · Apr 23, 2014 · Viewed 9.6k times · Source

Is there any short way to convert java.time.ZonedDateTime to XMLGregorianCalendar?

Maybe I need some intermediate steps like convert ZonedDateTime to java.util.Date, but that will make code too messy.

The problem appeared in JAX-WS web services, datetime there is passed as XMLGregorianCalendar.

Answer

Mikhail Boyarsky picture Mikhail Boyarsky · Apr 23, 2014

At the current moment I think it's the most straightforward way to do it:

ZonedDateTime now = ZonedDateTime.now();
GregorianCalendar gregorianCalendar = GregorianCalendar.from(now); 
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);