Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

How to convert XMLGregorianCalendar to Date to XMLGregorianCalendar in Java - Example Tutorial

There are several ways to convert XMLGregorianCalendar to Date in Java. You can convert XMLGregorianCalendar to either java.util.Date or java.sql.Date based upon your need. JAXB (Java API/Architecture for XML Bindings) is a popular framework to create XML documents from Java Objects and Java objects from XML files. JAXB also helps to create Java classes from XML Schema file (.XSD file). By default JAXB maps XSD data type xs:date, xs:time and xs:dateTime to XMLGregorianCalendar in Java, but you can configure XJC to create java.util.Date objects instead of javax.xml.datatype.XMLGregorianCalendar.

Since java.util.Date is the most popular way of dealing with date and time in Java, we often need to convert XMLGregorianCalendar instance to Date instance in Java. Thankfully by using Java API, we can easily do this conversion of XMLGregorianCalendar to Date and Date to XMLGregorianCalendar in Java. 

By the way, It's good to remember that XML Schema has three different types which can represent either date, time, or both, while java.util.Date contains date and time information together. In this Java and XML tutorial, we will see an example of converting XMLGregorianCalendar to Date and inverse in Java.

XMLGregorianCalendar to Date to XMLGregorianCalendar

Convert XMLGregorianCalendar to Date to XMLGregorianCalendar example Java tutorialHere is a Java program, which converts XMLGregorianCalendar instances to java.util.Date instances. We have two method, one which takes XMLGregorianCalendar and return java.util.Date, and other which takes Date and return XMLGregorianCalendar.

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

 * Java program to convert XMLGregorianCalendar to Date and inverse i.e. java.util.Date
 * to XMLGregorianCalendar. If you are using XJC to create Java classes from XML Schema
 * or XSD file, By default JAXB map XSD data types xs:date, xs:time and xs:dateTime
 * to XMLGregorianCalendar in Java.
 * @author Javin Paul
public class XMLCalendarToDate {

    public static void main(String args[]) {
       Date today = new Date();
       //Converting date to XMLGregorianCalendar in Java
       XMLGregorianCalendar xml = toXMLGregorianCalendar(today);
       System.out.println("XMLGregorianCalendar from Date in Java      : " + xml) ;
       //Converting XMLGregorianCalendar to java.util.Date in Java
       Date date = toDate(xml);
       System.out.println("java.util.Date from XMLGregorianCalendar in Java : " + date);
     * Converts java.util.Date to javax.xml.datatype.XMLGregorianCalendar
    public static XMLGregorianCalendar toXMLGregorianCalendar(Date date){
        GregorianCalendar gCalendar = new GregorianCalendar();
        XMLGregorianCalendar xmlCalendar = null;
        try {
            xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
        } catch (DatatypeConfigurationException ex) {
            Logger.getLogger(StringReplace.class.getName()).log(Level.SEVERE, null, ex);
        return xmlCalendar;
     * Converts XMLGregorianCalendar to java.util.Date in Java
    public static Date toDate(XMLGregorianCalendar calendar){
        if(calendar == null) {
            return null;
        return calendar.toGregorianCalendar().getTime();

XMLGregorianCalendar from Date in Java           : 2013-01-27T23:45:23.322-04:30
java.util.Date from XMLGregorianCalendar in Java : Sun Jan 27 23:45:23 VET 2013

Important points about XMLGregorianCalendar and Date

Few points which is worth knowing while converting XMLGregorianCalendar to Date in Java.

1) XML Schema has different data type for date, time and dateTime e.g. xsd:date, xsd:time and xsd:dateTime, By default JAXB XJC maps all these to XMLGregorianCalendar in Java.

2) It's possible to customize XJC to generate Date instead of XMLGregorianCalendar for xs:date, xs:time and xs:dateTime data types. I will write about that later, but you can still explores this option.

3) While creating instance of GregorianCalendar, its better to use constructor instead calling GregorianCalendar.getInstance() because it's similar to Calendar.getInstance() and can return different type of Calendar based upon Locale settings e.g. BuddhistCalendar for Thai locale or JapaneseImperialCalendar for Japan. By using constructor, you also removes type casting because getInstance() return instance of java.util.Calendar, and prevents ClassCastException in Java.

That's all on How to convert XMLGregorianCalendar to Date in Java and Date to XMLGregorianCalendar. Sometimes, it also helps to keep things simply by using formatted String to represent date and time in XML files and later convert String to Date in Java program.

Thanks for reading this article so far. If you find this article useful then please share with your friends and colleagues on Twitter, Facebook, or LinkedIn.


Pratibha said...

Hello Javin, I have a date field in my XML document , which has Date as String in MM/dd/yyyy format, Can you please let me know how can I convert that to util date in Java?

mazakuata said...

Gracias carnal, esta buena la explicación (y)

Post a Comment