I have the following date: 2011-08-12T20:17:46.384Z
. What format is this? I'm trying to parse it with Java 1.4 via DateFormat.getDateInstance().parse(dateStr)
and I'm getting
java.text.ParseException: Unparseable date: "2011-08-12T20:17:46.384Z"
I think I should be using SimpleDateFormat for parsing, but I have to know the format string first. All I have for that so far is yyyy-MM-dd
, because I don't know what the T
means in this string--something time zone-related? This date string is coming from the lcmis:downloadedOn
tag shown on Files CMIS download history media type.
The easy way to solve this is to replace the
"T"
with" "
and remove.384Z
.Below is an illustration of how to achieve that:
Not sure about the Java parsing, but that's ISO8601: http://en.wikipedia.org/wiki/ISO_8601
tl;dr
Standard ISO 8601 format is used by your input string.
ISO 8601
This format is defined by the sensible practical standard, ISO 8601.
The
T
separates the date portion from the time-of-day portion. TheZ
on the end is short forZulu
and means UTC.java.time
The old date-time classes bundled with the earliest versions of Java have proven to be poorly designed, confusing, and troublesome. Avoid them.
Instead, use the java.time framework built into Java 8 and later. The java.time classes supplant both the old date-time classes and the highly successful Joda-Time library.
The java.time classes use ISO 8601 by default when parsing/generating textual representations of date-time values.
The
Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds. That class can directly parse your input string without bothering to define a formatting pattern.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
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
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.The T is just a literal to separate the date from the time, and the Z means "zero hour offset" also known as "Zulu time" (UTC). If your strings always have a "Z" you can use:
Or using Joda Time, you can use
ISODateTimeFormat.dateTime()
.There are other ways to parse it rather than the first answer. To parse it:
(1) If you want to grab information about date and time, you can parse it to a
ZonedDatetime
(since Java 8) orDate
(old) object:or
(2) If you don't care the date and time and just want to treat the information as a moment in nanoseconds, then you can use
Instant
: