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

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

Calendar to XMLGregorianCalendar conversion & vice-versa:

It is very simple and easy to convert Calendar to XMLGregorianCalendar and viceversa

  • Convert Calendar to XMLGregorianCalendar
    • First, get Calendar instance
    • And then convert Calendar to GregorianCalendar
    • And then finally convert GregorianCalendar to XMLGregorianCalendar
  • Convert XMLGregorianCalendar to Calendar
    • First, create XMLGregorianCalendar object
    • And then convert XMLGregorianCalendar to GregorianCalendar using toGregorianCalendar() method
    • And then finally convert GregorianCalendar to Calendar using upcasting

1. Convert Calendar to XMLGregorianCalendar :

  • Get Calendar instance using getInstance() method of Calendar
  • For Calendar to XMLGregorianCalendar conversion,
    • First convert Calendar to GregorianCalendar by instantiating GregorianCalendar object and setting date/time using setTime() method of GregorianCalendar
    • And then convert GregorianCalendar to XMLGregorianCalendar using DatatypeFactory.newInstance().newXMLGregorianCalendar(gCal);
  • In short, Calendar -> GregorianCalendar -> XMLGregorianCalendar
  • Note: GregorianCalendar is a sub-class of Calendar class
  • Finally, print XMLGregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertCalendarToXmlGregorianCalendar.java

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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

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

public class ConvertCalendarToXmlGregorianCalendar {

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

		// 1. get Calendar instance
		Calendar calendar = Calendar.getInstance();
		System.out.println("Calendar is :- \n" 
				+ calendar);


		// 2. First, convert Calendar instance to GregorianCalendar object
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		gregorianCalendar.setTime(calendar.getTime());
		System.out.println("\nGregorianCalendar is :- \n" 
				+ gregorianCalendar);


		// 3. convert Calendar -> GregorianCalendar -> XMLGregorianCalendar 
		XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
				.newInstance()
				.newXMLGregorianCalendar(gregorianCalendar);
		System.out.println("\nXMLGregorianCalendar is :- \n" 
				+ xmlGregorianCalendar);


		// 3.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(
				xmlGregorianCalendar
				.toGregorianCalendar()
				.getTime()
				);
		System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 3.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(xmlGregorianCalendar
				.toGregorianCalendar()
				.getTime()
				);
		System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 3.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(
				xmlGregorianCalendar
				.toGregorianCalendar()
				.getTime()
				);
		System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

Calendar is :- 
java.util.GregorianCalendar[time=1661859964736,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=30,DAY_OF_YEAR=242,DAY_OF_WEEK=3,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=16,
SECOND=4,MILLISECOND=736,ZONE_OFFSET=19800000,DST_OFFSET=0]

GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661859964736,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=30,DAY_OF_YEAR=242,DAY_OF_WEEK=3,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=16,
SECOND=4,MILLISECOND=736,ZONE_OFFSET=19800000,DST_OFFSET=0]

XMLGregorianCalendar is :- 
2022-08-30T17:16:04.736+05:30

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
30-08-2022 17:16:04

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
30/Aug/22 17:16

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Tuesday 30 August, 2022 05:16:04 pm IST

2. Convert XMLGregorianCalendar to Calendar :

  • First, create XMLGregorianCalendar object
  • For XMLGregorianCalendar to Calendar conversion,
    • First convert XMLGregorianCalendar to GregorianCalendar using toGregorianCalendar() method
    • And then convert GregorianCalendar to Calendar by simply assigning GregorianCalendar object to Calendar instance using upcasting
  • In short, XMLGregorianCalendar -> GregorianCalendar -> Calendar
  • Note: Calendar is a super-class of GregorianCalendar class
  • Finally, print Calendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertXmlGregorianCalendarToCalendar.java

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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

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

public class ConvertXmlGregorianCalendarToCalendar {

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

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


		// 2. convert XMLGregorianCalendar -> GregorianCalendar -> Calendar
		GregorianCalendar gregorianCalendar = xmlGregorianCalendar.toGregorianCalendar();
		System.out.println("\nGregorianCalendar is :- \n" 
				+ gregorianCalendar);


		// 3. assign GregorianCalendar -> Calendar
		Calendar calendar = gregorianCalendar;
		System.out.println("\nCalendar is :- \n" 
				+ calendar);


		// 3.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(gregorianCalendar.getTime());
		System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 3.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(calendar.getTime());
		System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 3.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(calendar.getTime());
		System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

XMLGregorianCalendar/Date is :- 
2022-08-31T05:48:07.966+05:30

GregorianCalendar is :- 
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,
dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=1,
WEEK_OF_MONTH=1,DAY_OF_MONTH=31,DAY_OF_YEAR=1,DAY_OF_WEEK=5,
DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=5,MINUTE=48,
SECOND=7,MILLISECOND=966,ZONE_OFFSET=19800000,DST_OFFSET=0]

Calendar is :- 
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,
dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=1,
WEEK_OF_MONTH=1,DAY_OF_MONTH=31,DAY_OF_YEAR=1,DAY_OF_WEEK=5,
DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=5,MINUTE=48,
SECOND=7,MILLISECOND=966,ZONE_OFFSET=19800000,DST_OFFSET=0]

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
31-08-2022 05:48:07

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
31/Aug/22 05:48

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Wednesday 31 August, 2022 05:48:07 am IST

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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