XMLGregorianCalendar in java, with NO Timezone

Inquisitor Shm picture Inquisitor Shm · Nov 30, 2017 · Viewed 9k times · Source

How do I create an XMLGregorianCalendar without a timezone? No time offset (0) == UTC which outputs a 'Z' in the output. The meaning of my field is implicit local time, where locality is specified elsewhere on the xml record (such as address).

How do I create an XMLGregorianCalendar with undefined timezone (TimeZone indeterminate)?

Valid XML ISO-8601 Gregorian Calendar formats include:

  • CCYY-MM-DDThh:mm:ss – without Zulu time designator or TimeOffset refers to the local time of the relative physical location.
  • CCYY-MM-DDThh:mm:ssZ – the DateTime for the relative physical location is expressed in UTC (Zulu) time, for local time a conversion must take place.
  • CCYY-MM-DDThh:mm:ss+05:30 the DateTime for the relative physical location is expressed in some time zone which is +5 hours and 30 minutes offset from UTC. For local time we must first convert to UTC, then to local time by offsetting from UTC. There is no guarantee that the provided location is local time for the record location.

Answer

Ole V.V. picture Ole V.V. · Nov 30, 2017

I looked at the documentation of XMLGregorianCalendar. In the table at the top, the bottom row, it says that the timezone is

Number of minutes or DatatypeConstants.FIELD_UNDEFINED.

So let’s try the latter option:

    System.out.println(xcal);
    xcal.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    System.out.println(xcal);

In one test run this printed:

2017-11-30T07:54:05.647+01:00
2017-11-30T07:54:05.647

After I set the time zone to undefined, it no longer prints the offset. So I believe I have obtained what you want.