Guide to XMLGregorianCalendar in Java

Learn to convert XMLGregorianCalendar to Date class and format to string. Also, learn to apply timezone changes and daylight saving effects as well. This class is a representation of the W3C XML Schema 1.0 date/time datatypes that defines clear rules for specifying dates in XML format. 

1. Creating XMLGregorianCalendar

Using the constructor of XMLGregorianCalendar is not recommended and docs suggest using the DataTypeFactory to create its instance.

//Create XMLGregorianCalendar
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
XMLGregorianCalendar xCal = DatatypeFactory.newInstance()
    .newXMLGregorianCalendar(cal);

2. Converting XMLGregorianCalendar to Date

The GregorianCalendar.getTime() method returns the java.util.Date object.

XMLGregorianCalendar xCal = ..; //Create instance

Date date = xCal.toGregorianCalendar().getTime();
System.out.println(date);

Program Output.

Thu Feb 17 23:29:25 IST 2022

3. Formatting XMLGregorianCalendar

The simplest way to format XMLGregorianCalendar is to first convert it to the Date object, and format the Date to String.

XMLGregorianCalendar xCal = ..; //Create instance

Date date = xCal.toGregorianCalendar().getTime();

DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z");
String formattedString = df.format(date);
System.out.println(formattedString);

Program Output.

02/17/2022 11:34 pm IST

4. Formatting in Another Timezone

If you want to print the formatted date in another timezone, such as GMT, then we will need to set timezone is DateFormat instance.

XMLGregorianCalendar xCal = ..; //Create instance
Date date = xCal.toGregorianCalendar().getTime();

DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z");
df.setTimeZone(TimeZone.getTimeZone("GMT"));

String formattedString = df.format(date);
System.out.println(formattedString);

Program Output.

02/17/2022 06:13 pm GMT

5. Handling Daylight Saving Time (DST) Effect

To check whether current time or adjusted time falls under DST (daylight saving time), then you might want to handle those changes as well.

This will be useful when we are converting a Date from one timezone to another timezone, and there may be DST effects on those timezones.

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

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

public class Main {

	public static void main(String[] args) throws DatatypeConfigurationException {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(new Date());
		XMLGregorianCalendar xCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);

        System.out.println(convertXmlGregorianToString(xCal));
	}

	public static String convertXmlGregorianToString(XMLGregorianCalendar xc)
	{
		TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
		TimeZone fromTimeZone = TimeZone.getDefault();
		GregorianCalendar gCalendar = xc.toGregorianCalendar();
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z");
		Date date = adjustToTimezone(gCalendar.getTime(), fromTimeZone, gmtTimeZone);
		String dateString = df.format(date);
		return dateString;
	}

	public static Date adjustToTimezone(Date date, TimeZone fromZone, TimeZone toZone)
	{
		Date adjustedToTimezone = new Date(date.getTime() + toZone.getRawOffset() - fromZone.getRawOffset());
		// Is the adjusted date in Daylight savings?
		if (fromZone.inDaylightTime(adjustedToTimezone) != toZone.inDaylightTime(adjustedToTimezone)) {
			adjustedToTimezone = new Date(adjustedToTimezone.getTime() + toZone.getDSTSavings() - fromZone.getDSTSavings());
		}
		return adjustedToTimezone;
	}
}

Program Output.

04/11/2017 07:08 AM IST

Happy Learning !!

Sourcecode Download

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial