Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Datatype Conversion in Java: XMLGregorianCalendar to java.util.Date / java.util.Date to XMLGregorianCalendar

DZone's Guide to

Datatype Conversion in Java: XMLGregorianCalendar to java.util.Date / java.util.Date to XMLGregorianCalendar

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

package singz.test;

import java.util.Date;
import java.util.GregorianCalendar;

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

/**
* A utility class for converting objects between java.util.Date and
* XMLGregorianCalendar types
*
*/
public class XMLGregorianCalendarConversionUtil {

// DatatypeFactory creates new javax.xml.datatype Objects that map XML
// to/from Java Objects.
private static DatatypeFactory df = null;

static {
try {
df = DatatypeFactory.newInstance();
} catch(DatatypeConfigurationException e) {
throw new IllegalStateException(
"Error while trying to obtain a new instance of DatatypeFactory", e);
}
}

// Converts a java.util.Date into an instance of XMLGregorianCalendar
public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
if(date == null) {
return null;
} else {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date.getTime());
return df.newXMLGregorianCalendar(gc);
}
}

// Converts an XMLGregorianCalendar to an instance of java.util.Date
public static java.util.Date asDate(XMLGregorianCalendar xmlGC) {
if(xmlGC == null) {
return null;
} else {
return xmlGC.toGregorianCalendar().getTime();
}
}

public static void main(String[] args) {
Date currentDate = new Date(); // Current date

// java.util.Date to XMLGregorianCalendar
XMLGregorianCalendar xmlGC = XMLGregorianCalendarConversionUtil.asXMLGregorianCalendar(
currentDate);
System.out.println(
"Current date in XMLGregorianCalendar format: " + xmlGC.toString());

// XMLGregorianCalendar to java.util.Date
System.out.println(
"Current date in java.util.Date format: " +
XMLGregorianCalendarConversionUtil.asDate(xmlGC).toString());
}
}

Why do we need XMLGregorianCalendar?

Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects.

In the default data type bindings i.e. mappings of XML Schema (XSD) data types to Java data types in JAXB, the following types in XML schema (mostly used in web services definition) – xsd:dateTime, xsd:time, xsd:date and so on map to javax.xml.datatype.XMLGregorianCalendar Java type.

 

From http://singztechmusings.in/datatype-conversion-in-java-xmlgregoriancalendar-to-java-util-date-java-util-date-to-xmlgregoriancalendar/

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,xml ,classes

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}