Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll discuss LocalDate and XMLGregorianCalendar and provide examples of converting between the two types.

2. XMLGregorianCalendar

The XML Schema standard defines clear rules for specifying dates in XML format. In order to use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes.

3. LocalDate

A LocalDate instance represents a date without a timezone in the ISO-8601 calendar system. As a result, LocalDate is, for example, suitable for storing birthdays but not anything related to time. Java introduced LocalDate in version 1.8.

4. From LocalDate to XMLGregorianCalendar

First, we’ll see how to convert from LocalDate to XMLGregorianCalendar. In order to generate a new instance of XMLGregorianCalendar, we use a DataTypeFactory from the javax.xml.datatype package.

So, let’s create an instance of LocalDate and convert it to XMLGregorianCalendar:

LocalDate localDate = LocalDate.of(2019, 4, 25);

XMLGregorianCalendar xmlGregorianCalendar = 
  DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString());

assertThat(xmlGregorianCalendar.getYear()).isEqualTo(localDate.getYear());
assertThat(xmlGregorianCalendar.getMonth()).isEqualTo(localDate.getMonthValue());
assertThat(xmlGregorianCalendar.getDay()).isEqualTo(localDate.getDayOfMonth());
assertThat(xmlGregorianCalendar.getTimezone()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);

As previously noted, an XMLGregorianCalendar instance has the possibility of having timezone information. However, LocalDate doesn’t have any information about time.

Therefore, when we perform the conversion, the timezone value will remain as FIELD_UNDEFINED.

5. From XMLGregorianCalendar to LocalDate

Likewise, we’ll now see how to perform the conversion the other way around. As it turns out, converting from a XMLGregorianCalendar to LocalDate is much easier.

Again, since LocalDate does not have information about time, a LocalDate instance can contain only a subset of the XMLGregorianCalendar information.

Let’s create an instance of XMLGregorianCalendar and perform the conversion:

XMLGregorianCalendar xmlGregorianCalendar = 
  DatatypeFactory.newInstance().newXMLGregorianCalendar("2019-04-25");

LocalDate localDate = LocalDate.of(
  xmlGregorianCalendar.getYear(), 
  xmlGregorianCalendar.getMonth(), 
  xmlGregorianCalendar.getDay());

assertThat(localDate.getYear()).isEqualTo(xmlGregorianCalendar.getYear());
assertThat(localDate.getMonthValue()).isEqualTo(xmlGregorianCalendar.getMonth());
assertThat(localDate.getDayOfMonth()).isEqualTo(xmlGregorianCalendar.getDay());

6. Conclusion

In this quick tutorial, we’ve covered the transformations between LocalDate instances and XMLGregorianCalendar, and vice-versa.

And, as always, the sample code is available over on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE