Search code examples
javaxmldatetimexsdxmlbeans

XMLBeans dates without timezone information


This question is related to: XmlBeans XmlDateTime format without timezone info

But I need to print a date in XMLBeans without any timezone info. What I read from World Wide Web consortium is:

A "date object" is an object with year, month, and day properties just like those of dateTime objects, plus an optional timezone-valued timezone property

http://www.w3.org/TR/xmlschema-2/#date

The key word is optional. I need to remove it from default XMLBeans behaviour.

What I have tried:

XmlDateTime xmlDateTime = XmlDateTime.Factory.newInstance();
xmlDateTime.setCalendarValue(new GregorianCalendar());
GDateBuilder gdb = new GDateBuilder(xmlDateTime.getDateValue());
gdb.normalize();
xmlDateTime.setGDateValue(gdb.toGDate());
instruction.setReqdExctnDt(gdb.getCalendar());
CBIPartyIdentification4 partyId = CBIPartyIdentification4.Factory.newInstance();

What I get is 2014-05-16Z what I need is 2014-05-16.

How can I do this?


Solution

  • Use the class XmlCalendar, which won't add time zone information:

    XmlDateTime xmlDateTime = XmlDateTime.Factory.newInstance();
    Calendar xmlCalendar = new XmlCalendar();
    xmlCalendar.set(Calendar.YEAR, 2014);
    xmlCalendar.set(Calendar.MONTH, 2);
    xmlCalendar.set(Calendar.DATE, 22);
    xmlDateTime.setCalendarValue(xmlCalendar);