Open In App
Related Articles

Converting XMLGregorianCalendar to Date in Java

Like Article
Save Article
Report issue

XMLGregorianCalendar can be transformed into either java.util.Date or java.sql.Date as per the requirements. JAXB (Java API/XML Bindings Architecture) is a common framework for creating XML documents from Java objects and XML files from Java objects. JAXB also allows the XML Schema file to construct Java classes (.XSD file). JAXB maps xs:date, xs:time and xs:dateTime to XMLGregorianCalendar in Java by default, but you can customize XJC to construct java.util.Date objects instead of javax.xml.datatype.xmlGregorianCalendar.

Since java.util.Date is the most common way to deal with Java date and time, we always need to convert the instance of XMLGregorianCalendar to the Java Date instance.

Using the Java API, we can easily convert XMLGregorianCalendar to XMLGregorianCalendar Date and Date in Java.

By the way, it’s nice to note that there are three different forms of XML Schema that can represent either date, time, or both, while java.util.Date includes details about date and time together.

Here is a Java program, which converts XMLGregorianCalendar to Date. So we will take XMLGregorianCalendar and return java.util.Date.



// Convert XMLGregorianCalendar to Date in Java
import java.util.*;
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;
class GFG {
    public static void main(String[] args)
        Date today = new Date();
        // Converting date to XMLGregorianCalendar
        XMLGregorianCalendar xml
            = toXMLGregorianCalendar(today);
            "XMLGregorianCalendar from Date in Java      : "
            + xml);
        // Converting XMLGregorianCalendar to java.util.Date
        // in Java
        Date date = toDate(xml);
            "java.util.Date from XMLGregorianCalendar in Java : "
            + date);
    public static XMLGregorianCalendar
    toXMLGregorianCalendar(Date date)
        GregorianCalendar gCalendar
            = new GregorianCalendar();
        XMLGregorianCalendar xmlCalendar = null;
        try {
                = DatatypeFactory.newInstance()
        catch (DatatypeConfigurationException ex) {
        return xmlCalendar;
    public static Date toDate(XMLGregorianCalendar calendar)
        if (calendar == null) {
            return null;
        return calendar.toGregorianCalendar().getTime();


XMLGregorianCalendar from Date in Java      : 2021-02-22T17:10:28.732Z
java.util.Date from XMLGregorianCalendar in Java : Mon Feb 22 17:10:28 UTC 2021

So now the important points about XMLGregorianCalendar and date:

  • XML Schema has various date, time and dateTime data types, e.g. xsd:date, xsd:time and xsd:dateTime, all of which are mapped to XMLGregorianCalendar in Java by default with JAXB XJC.
  • When constructing a GregorianCalendar case, it is best to use a constructor instead of calling GregorianCalendar.getInstance() since it is similar to Calendar.getInstance() and can return different calendar types based on local settings, e.g. Thai Local Buddhist Calendar or Japanese Imperial Japan Imperial Calendar. You also exclude typecasting when using a constructor since getInstance() returns the java.util.Calendar instance and prevents ClassCastException in Java.
  • For xs:date, xs:time and xs:dateTime data types, you can configure XJC to generate Date instead of XMLGregorianCalendar. I’ll write about that later, but this option can still be explored.

This is all about how to convert the XMLGregorianCalendar to Java Date and XMLGregorianCalendar Date.

Last Updated : 03 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads