Java 8 – How to convert Date to XMLGregorianCalendar and vice-versa ?

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

Date to XMLGregorianCalendar conversion & vice-versa:

It is very simple and easy to convert Date to XMLGregorianCalendar via GregorianCalendar

  • Convert Date to XMLGregorianCalendar using setTime() method and DatatypeFactory object
    • First, get Date object
    • And then instantiate GregorianCalendar & pass date object in setTime() method of GregorianCalendar
    • And then pass converted GregorianCalendar object to DatatypeFactory.newInstance().newXMLGregorianCalendar(gCal); which will return XMLGregorianCalendar
  • Convert XMLGregorianCalendar to Date using toGregorianCalendar() & getTime() methods
    • First, get XMLGregorianCalendar object
    • And then invoke toGregorianCalendar() & getTime() methods subsequently on the above obtained XMLGregorianCalendar object

1. Convert Date to XMLGregorianCalendar :

  • For conversion of Date to XMLGregorianCalendar use DatatypeFactory
  • DatatypeFactory.newInstance().newXMLGregorianCalendar(gCal) returns XMLGregorianCalendar object
    • First, convert Date to GregorianCalendar using setTime() method
    • Pass converted GregorianCalendar object to DatatypeFactory.newInstance().newXMLGregorianCalendar() method which returns XMLGregorianCalendar
  • In short, Date -> GregorianCalendar -> XMLGregorianCalendar
  • Lets see an example for conversion of Date to XMLGregorianCalendar in the below illustration

ConvertJavaUtilDateToXmlGregorianCalendar.java

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

import java.util.Date;
import java.util.GregorianCalendar;

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

public class ConvertJavaUtilDateToXmlGregorianCalendar {

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

		// 1. get current date/time using java.util.Date
		Date date = new Date();
		System.out.println("Current Date/time is :- \n" 
				+ date);


		// 2. First, convert Date to GregorianCalendar
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		gregorianCalendar.setTime(date);
		System.out.println("\nGregorianCalendar is :- \n" 
				+ gregorianCalendar);


		// 3. print date/time from Calendar
		System.out.println("\nGregorianCalendar Date/time is :- \n" 
				+ gregorianCalendar.getTime());


		// 4. convert Date -> GregorianCalendar -> XmlGregorianCalendar
		XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
				.newInstance()
				.newXMLGregorianCalendar(gregorianCalendar);
		System.out.print("\nXMLGregorianCalendar Date/time is :- \n" 
				+ xmlGregorianCalendar);
	}
}

Output:

Current Date/time is :- 
Mon Aug 29 14:00:58 IST 2022

GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661761858367,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=36,
WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=241,DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=0,
SECOND=58,MILLISECOND=367,ZONE_OFFSET=19800000,DST_OFFSET=0]

GregorianCalendar Date/time is :- 
Mon Aug 29 14:00:58 IST 2022

XMLGregorianCalendar Date/time is :- 
2022-08-29T14:00:58.367+05:30

2. Convert XMLGregorianCalendar to Date :

  • First, create XMLGregorianCalendar object using DatatypeFactory.newInstance().newXMLGregorianCalendar(String) method
  • For conversion of XMLGregorianCalendar to Date,
    • First, convert XMLGregorianCalendar to GregorianCalendar using toGregorianCalendar() method
    • And then invoke getTime() method to convert GregorianCalendar to Date
  • In short, XMLGregorianCalendar -> GregorianCalendar -> Date
  • Lets see an example for conversion of XMLGregorianCalendar to Date in the below illustration

ConvertXmlGregorianCalendarToJavaUtilDate.java

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

import java.util.Date;

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

public class ConvertXmlGregorianCalendarToJavaUtilDate {

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

		// 1. get new instance of XMLGregorianCalendar
		XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
				.newInstance()
				.newXMLGregorianCalendar(
						"2022-08-29T11:48:07.966+05:30"
						);
		System.out.println("XMLGregorianCalendar/Date is :- \n" + xmlGregorianCalendar);


		// 2. convert XMLGregorianCalendar to Date
		Date date = xmlGregorianCalendar // XMLGregorianCalendar
				.toGregorianCalendar() // GregorianCalendar
				.getTime(); // Date


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

Output:

XMLGregorianCalendar/Date is :- 
2022-08-29T11:48:07.966+05:30

Conversion of XMLGregorianCalendar to Date is :- 
Mon Aug 29 11:48:07 IST 2022

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to get Date/time with AM/PM marker and Zone ?
Java 8 – How to convert Date to GregorianCalendar and vice-versa ?