How to format XMLGregorianCalendar
format XMLGregorianCalendar
java
XMLGregorianCalendar formatter
Recently, I encountered an problem with formatting the date for a SOAP Web Service.
Description
When xs:dateTime
is used to define a date or datetime field for an SOAP Web Service, wsimport
tool will use in Java client classes XMLGregorianCalendar
object and the default formatting for XMLGregorianCalendar
object is yyyy-MM-dd'T'HH:mm:ss.SSSXXX.
My service needed the date formatted without milliseconds and timezone. There are several options to do this. Here are 2 of them.
Option 1
Using SimpleDateFormat class and pass the date in string format.
String FORMATER = "yyyy-MM-dd'T'HH:mm:ss";
DateFormat format = new SimpleDateFormat(FORMATER);
Date date = new Date();
XMLGregorianCalendar gDateFormatted1 =
DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(date));
Option 2
Passing the all the date component separately and use DatatypeConstants.FIELD_UNDEFINED
for timezone and milliseconds parameters.
Calendar cal = Calendar.getInstance();
XMLGregorianCalendar gDateFormatted2 =
DatatypeFactory.newInstance().newXMLGregorianCalendar(
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
cal.get(Calendar.SECOND), DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
Full Example
package com.admfactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class XMLGregorianCalendarFormatter {
public static void main(String[] args) throws Exception {
System.out.println("XMLGregorianCalendar Formatter tutorial");
System.out.println();
GregorianCalendar gCal = new GregorianCalendar();
gCal.setTime(new Date());
XMLGregorianCalendar gDateUnformated = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCal);
System.out.println("Unformatted date: " + gDateUnformated);
System.out.println();
String FORMATER = "yyyy-MM-dd'T'HH:mm:ss";
DateFormat format = new SimpleDateFormat(FORMATER);
Date date = new Date();
XMLGregorianCalendar gDateFormatted1 =
DatatypeFactory.newInstance().newXMLGregorianCalendar(format.format(date));
System.out.println("Formatted using SimpleDateFormat: " + gDateFormatted1.toString());
Calendar cal = Calendar.getInstance();
XMLGregorianCalendar gDateFormatted2 =
DatatypeFactory.newInstance().newXMLGregorianCalendar(
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
cal.get(Calendar.SECOND), DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
System.out.println("Formatted using FIELD_UNDEFINED: " + gDateFormatted2);
}
}
Output
XMLGregorianCalendar Formatter tutorial
Unformatted date: 2016-05-10T10:23:06.510+01:00
Formatted using SimpleDateFormat: 2016-05-10T10:23:06
Formatted using FIELD_UNDEFINED: 2016-05-10T10:23:06
Conclusions
This is a simple solution to format the XMLGregorianCalendar
output if you need to remove last 2 fields. If you want to avoid using it and convert to Date or Calendar, you need to use the bindings
and create a Java class to handle the conversion. For more information about date formatting, follow the How to get date and time in Java tutorial.