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

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

GregorianCalendar to XMLGregorianCalendar conversion & vice-versa:

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

  • Convert GregorianCalendar to XMLGregorianCalendar
    • First, create GregorianCalendar object
    • And then convert GregorianCalendar to XMLGregorianCalendar
  • Convert XMLGregorianCalendar to GregorianCalendar
    • First, create XMLGregorianCalendar object
    • And then convert XMLGregorianCalendar to GregorianCalendar using toGregorianCalendar() method

1. Convert GregorianCalendar to XMLGregorianCalendar :

  • Get GregorianCalendar object by instantiating
  • For GregorianCalendar to XMLGregorianCalendar conversion,
    • use DatatypeFactory.newInstance().newXMLGregorianCalendar(gCal);
  • In short, GregorianCalendar -> XMLGregorianCalendar
  • Finally, print XMLGregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertGregorianCalendarToXmlGregorianCalendar.java

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

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

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

public class ConvertGregorianCalendarToXmlGregorianCalendar {

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

		// 1. get GregorianCalendar object
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		System.out.println("GregorianCalendar is :- \n" +
				gregorianCalendar);


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


		// 2.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);


		// 2.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);


		// 2.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:

GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661860916928,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=31,
SECOND=56,MILLISECOND=928,ZONE_OFFSET=19800000,DST_OFFSET=0]

XMLGregorianCalendar is :- 
2022-08-30T17:31:56.928+05:30

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

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

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

2. Convert XMLGregorianCalendar to GregorianCalendar :

  • First, create XMLGregorianCalendar object
  • For XMLGregorianCalendar to GregorianCalendar conversion,
    • Convert XMLGregorianCalendar to GregorianCalendar using toGregorianCalendar() method
  • In short, XMLGregorianCalendar -> GregorianCalendar
  • Finally, print GregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertXmlGregorianCalendarToGregorianCalendar.java

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

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

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

public class ConvertXmlGregorianCalendarToGregorianCalendar {

	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
		GregorianCalendar gregorianCalendar = xmlGregorianCalendar.toGregorianCalendar();
		System.out.println("\nGregorianCalendar is :- \n" 
				+ gregorianCalendar);


		// 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(gregorianCalendar.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(gregorianCalendar.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]

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 sort List by LocalTime in 4 ways ?
Java 8 – How to convert Calendar to XMLGregorianCalendar and vice-versa ?