Convert between LocalDate and XMLGregorianCalendar

maja picture maja · Apr 21, 2015 · Viewed 64.1k times · Source

What's the best way to convert between LocalDate from Java 8 and XMLGregorianCalendar?

Answer

JodaStephen picture JodaStephen · Apr 21, 2015

Converting from LocalDate to XMLGregorianCalendar:

LocalDate date = LocalDate.now();
GregorianCalendar gcal = GregorianCalendar.from(date.atStartOfDay(ZoneId.systemDefault()));
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);

Converting back is simpler:

xcal.toGregorianCalendar().toZonedDateTime().toLocalDate();