Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?

In this article, we will learn how to convert LocalDate to XMLGregorianCalendar in Java 1.8 version and vice-versa

1. Convert LocalDate to XMLGregorianCalendar :

  • For conversion of LocalDate to XMLGregorianCalendar use DatatypeFactory
  • DatatypeFactory.newInstance().newXMLGregorianCalendar(String) returns XMLGregorianCalendar object
  • Lets see an example for conversion of LocalDate to XMLGregorianCalendar in the below illustration

ConvertLocalDateToXmlGregorianCalendar.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class ConvertLocalDateToXmlGregorianCalendar {

	public static void main(String[] args) throws DatatypeConfigurationException {

		// 1. get current System Date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System LocalDate is :- \n" + localDate);


		// 2. convert LocalDate to XMLGregorianCalendar
		XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
				.newInstance()
				.newXMLGregorianCalendar(
						localDate.toString()
						);


		// 3. print to console
		System.out.print("\nConversion of LocalDate to XMLGregorianCalendar is :- \n"
				+ xmlGregorianCalendar);
	}
}

Output:

Current System LocalDate is :- 
2022-08-02

Conversion of LocalDate to XMLGregorianCalendar is :- 
2022-08-02

2. Convert XMLGregorianCalendar to LocalDate :

  • First, create XMLGregorianCalendar object using DatatypeFactory.newInstance().newXMLGregorianCalendar(String) method
  • For conversion of XMLGregorianCalendar to LocalDate, use one of the variants of LocalDate.of() method passing Year/Month/DayOfMonth values extracted from newly created XMLGregorianCalendar object
  • Lets see an example for conversion of XMLGregorianCalendar to LocalDate in the below illustration

ConvertXmlGregorianCalendarToLocalDate.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class ConvertXmlGregorianCalendarToLocalDate {

	public static void main(String[] args) throws DatatypeConfigurationException {

		// 1. get new instance of XMLGregorianCalendar
		XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
				.newInstance()
				.newXMLGregorianCalendar(
						"2021-11-23"
						);
		System.out.println("XMLGregorianCalendar/Date is :- \n" + xmlGregorianCalendar);


		// 2. convert XMLGregorianCalendar to LocalDate
		LocalDate localDate = LocalDate.of(
				xmlGregorianCalendar.getYear(), 
				xmlGregorianCalendar.getMonth(), 
				xmlGregorianCalendar.getDay()
				);


		// 3. print to console
		System.out.print("\nConversion of XMLGregorianCalendar to LocalDate is :- \n"
				+ localDate);
	}
}

Output:

XMLGregorianCalendar/Date is :- 
2021-11-23

Conversion of XMLGregorianCalendar to LocalDate is :- 
2021-11-23

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert java.util.Date to LocalDate in different ways ?
Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?