-1

1) If I print out a calendar object I get: java.util.GregorianCalendar[time=1567535353679,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=8,WEEK_OF_YEAR=36,WEEK_OF_MONTH=1,DAY_OF_MONTH=3,DAY_OF_YEAR=246,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=6,HOUR_OF_DAY=18,MINUTE=29,SECOND=13,MILLISECOND=679,ZONE_OFFSET=0,DST_OFFSET=0]

2) If I print out a Date object I get:

Tue Sep 03 18:29:13 GMT 2019

3) However I am examining the XML generated and I observe this format:

2014-02-28T08:00:00.000Z

Which format am I seeing in the XML ?

JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
  • Possible duplicate of [Convert Java Date into XML Date Format (and vice versa)](https://stackoverflow.com/questions/4099862/convert-java-date-into-xml-date-format-and-vice-versa) – Yoshikage Kira Sep 03 '19 at 18:47
  • What do you mean by "examining the XML generated"? What tool or class? – Basil Bourque Sep 03 '19 at 18:51
  • 2
    I recommend that you use neither `Date` nor `Calendar`. Those classes are poorly designed and long outdated. Instead use classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). `OffsetDateTime` and `Instant` have `toString` methods that can give you the format you observed in XML. – Ole V.V. Sep 03 '19 at 19:19

2 Answers2

4

Date-time objects have no “format”

How to identify date format?

Date-time objects do not have a “format”. Each class defines its own mechanism for internally storing the date-time value. That mechanism is generally none of our business. The API of the class, the promises made by that class, are all that matters.

You may be conflating the text generated by a date-time object with the date-time object itself. Date-time classes can parse text as part of their instantiation. And date-time classes can generate text to represent their value. But the text and the object are separate and distinct.

Avoid Calendar

If I print out a calendar object I get:

The Calendar::toString method is just a data-dump of many of its internal fields. Meant only for debugging obviously, not useful otherwise.

The Calendar class and its commonly-used subclass GregorianCalendar are both now legacy. They were supplanted years ago by the java.time classes as of the adoption of JSR 310. Never use them. GregorianCalendar was specifically replaced by ZonedDateTime.

Avoid Date

If I print out a Date object I get:

The Date::toString method lies. That method dynamically applies the JVM’s current default time zone while generating text. A java.util.Date actually represents a moment in UTC, not the time zone seen in the result of toString. This anti-feature is one of many reasons to never use Date.

The java.util.Date class was replaced by java.time.Instant, representing a moment in UTC as well but with a finer resolution of nanoseconds versus milliseconds.

ISO 8601

However I am examining the XML generated and I observe this format:

2014-02-28T08:00:00.000Z

The format of that text is defined in the ISO 8601 standard. This standard is wisely designed to represent a variety of date-time values textually for data-exchange. This standard supplants earlier poorly-defined date-time text formats, such as seen in the early email protocols.

The java.time classes use ISO 8601 formats by default when parsing/generating strings.

Current moment in UTC

To capture the current moment in UTC, use Instant.

Instant instant = Instant.now() ;  // Capture the current moment in UTC.

Generate text representing that value in standard ISO 8601 format. The Z on the end means UTC, and is pronounced “Zulu”.

2020-01-23T12:34:56.123456Z

Parse such a string.

Instant instant = Instant.parse( "2020-01-23T12:34:56.123456Z" );

Time zone

See that same moment through the wall-clock time used by the people of a particular region, a time zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Generate a string in a format that wisely extends the ISO 8601 standard by appending the name of the time zone in square brackets.

String output = zdt.toString() ;

See this code run live at IdeOne.com.

instant.toString(): 2020-01-23T12:34:56.123456Z

zdt.toString(): 2020-01-23T07:34:56.123456-05:00[America/Montreal]

Date-time types in Java

Table of date-time types in Java (both legacy and modern) and in standard SQL.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • That was a lot of text to answer the simple question "Which format am I seeing in the XML ?", which is the only question posed, and then you didn't even get it right. The date format used in XML is defined by the XML Schema data types specification ([XML Schema Part 2: Datatypes Second Edition](https://www.w3.org/TR/xmlschema-2/)), and the [`dateTime`](https://www.w3.org/TR/xmlschema-2/#dateTime) data type is "closely related to" / "inspired by" ISO 8601, so not actually ISO 8601. – Andreas Sep 03 '19 at 19:15
  • 1
    @Andreas Actually that "XML format" is only 1/4th of the Question, but thanks for the feedback. As for the context of that string in XML, the Question makes no mention of XML Schema being involved, and did not even explain where that string came from. Indeed, I posted a Comment on the Question asking for that explanation. – Basil Bourque Sep 03 '19 at 19:56
  • Actually, there is only only question, in the last line. The rest are statements of facts, not questions. If OP meant all of them to be questions, then OP should learn [how to ask a better question](https://stackoverflow.com/help/how-to-ask). – Andreas Sep 03 '19 at 20:21
  • @Andreas I must read the Question differently than you. – Basil Bourque Sep 03 '19 at 20:24
2

Which format am I seeing in the XML ?

The XML format is inspired by ISO 8601, as specified by the XML schema documentation.

3.2.7.1 Lexical representation

The ·lexical space· of dateTime consists of finite-length sequences of characters of the form: '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247