Reputation: 101
I'm using OffsetDateTime class for parsing date from the database. Why the date contains 'T' ??
Example: 2018-01-01T12:00:00.000Z
I can understand the format is <<Date>>T<<Time>>.<<Offset>><<TimeZone>>. But still, I don't understand why 'T' is used in between.
Thanks in advance
Upvotes: 2
Views: 899
Reputation: 338476
2018-01-23T12:00:00.000Z
That means noon on the twenty-third of January in 2018 in UTC.
That is one of the standard formats defined by ISO 8601. These formats are designed for exchanging date-time values as text, easy to parse by machine, easy to read by humans across cultures. The ISO 8601 is a modern standard, supplanting formats seen in various earlier Internet standards. ISO 8601 is also being adopted in various industries beyond information technology.
T
in the middle separates the date portion from the time-of-day portion. Z
on the end is pronounced “Zulu” and means an offset-from-UTC of zero hours-minutes-seconds, or +00:00:00
.why 'T' is used in between
A letter was desired to avoid the use of SPACE characters to make recognition and parsing easier. As to why T
, you’d have to ask the authors of the standard. I can only guess that the letter “t” was chosen for the word time in English, temps in French, etc.
The use of Z
for an offset of zero is taken from common usage in aviation and military.
I highly recommend you use these ISO 8601 formats when storing, writing, exchanging, and logging date-time values as text. These formats are wisely designed to be simple and practical. They avoid the use of SPACE character, and avoid extensive use of English and any other language.
The java.time classes built into Java 8 and later use these formats by default when parsing and generating strings.
Instant.now().toString()
2019-08-27T20:15:21.005946Z
The java.time.ZonedDateTime
class extends the standard wisely to append the name of the time zone in square brackets.
Instant instant = Instant.parse( "2019-08-27T20:15:21.005946Z" ) ;
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ; // Same moment, same point on the timeline, different wall-clock time.
zdt.toString(): 2019-08-28T08:15:21.005946+12:00[Pacific/Auckland]
You asked:
Am I able to use 'W' instead of 'T' in my given example
No. When representing a moment, you must use a T
between the date and time.
A W
is used in another of the ISO 8601 formats, when representing a week date of a week-based year.
String output = zdt.toLocalDate().format( DateTimeFormatter.ISO_WEEK_DATE ;
2019-W35-3
For working with standard weeks, use the YearWeek
class from the ThreeTen-Extra project that extends java.time functionality.
LocalDate ld = zdt.toLocalDate() ;
YearWeek yw = YearWeek.from( ld ) ;
ld.toString(): 2019-08-28
yw.toSting(): 2019-W35
LocalDate ld = zdt.toLocalDate() ; // Extract just the date without the time-of-day and without time zone.
See all that code above run live at IdeOne.com, except for YearWeek
.
Upvotes: 3