Convert XMLGregorianCalendar to date time – Format to string

Learn to convert XMLGregorianCalendar to Date class or string value. Also learn to apply timezone changes and daylight saving effects as well.

Table of Contents

1. Convert XMLGregorianCalendar to Date or String
2. Adjust between timezones
3. Apply daylight saving effect

Convert XMLGregorianCalendar to Date or String

Java program to convert XMLGregorianCalendar to Date object and format XMLGregorianCalendar to string value.

public class Main {
	
	public static void main(String[] args) throws DatatypeConfigurationException 
	{
		GregorianCalendar cal = new GregorianCalendar();

		//Set todays date
		cal.setTime(new Date());
		XMLGregorianCalendar xCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
        
        System.out.println(convertXmlGregorianToString(xCal));
	}

	public static String convertXmlGregorianToString(XMLGregorianCalendar xc) 
	{
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z");

		GregorianCalendar gCalendar = xc.toGregorianCalendar();

		//Converted to date object
		Date date = gCalendar.getTime();

		//Formatted to String value
		String dateString = df.format(date);

		return dateString;
	}
}

Program Output.

04/11/2017 12:21 PM IST

2. XMLGregorianCalendar to Date with timezone

2.1. Timezone adjusted local date time

Adjust XMLGregorianCalendar date from default timezone to GMT timezone.

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());
		return adjustedToTimezone;
	}
}

Program Output.

04/11/2017 07:08 AM IST

2.2. Date time formatted to differnt timezone

Please note that converted time’s timezone is printed as IST, though its value is adjusted to GMT. If you want to print the timezone as GMT then you will need to set timezone is DateFormat instance.

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

Program Output.

04/11/2017 07:08 AM GMT

3. XMLGregorianCalendar with daylight saving 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.

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

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.