What\'s the best way to convert between LocalDate from Java 8 and XMLGregorianCalendar?
The following is a simple way to convert from LocalDate to XMLGregorianCalendar which both preserves the undefined fields (hours, timezone, etc.) and is efficient (i.e. no conversion to/from String). Unlike some of the other solutions this results in XML dates without timezones, e.g. instead of .
LocalDate date = ...;
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xmlCal.setYear(date.getYear());
xmlCal.setMonth(date.getMonthValue());
xmlCal.setDay(date.getDayOfMonth());
Converting back is a bit simpler:
XMLGregorianCalendar xmlCal = ...
LocalDate date = LocalDate.of(xmlCal.getYear(), xmlCal.getMonth(), xmlCal.getDay());