This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

What is the date format? 2011-08-12T20:17:46.384z >

Ask questions

I have the following date: 2011-08-12T20:17:46.384z.

What format is this?

I’m trying to parse it through Java 1.4,

The DateFormat. GetDateInstance (). The parse (dateStr) and got it

Java.text. ParseException: Date that cannot be parsed:"2011-08- 12 t20:17:46.384 z"Copy the code

I think I should use SimpleDateFormat

Parse, but I have to know the format string first. So far yyyY-MM-DD, all I have is, because I don’t know what the string T means – time zone dependent? This date string comes from the label displayed on the file CMIS download history media type lCMis :downloadedOn.

Answer a:

T is simply a word separating date and time, and Z stands for “zero hour offset,” also known as “Zulu Time” (UTC). If your string always has a “Z”, you can use:

SimpleDateFormat format = new SimpleDateFormat(
    "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Copy the code

Or use Joda Time

You can use ISODateTimeFormat. DateTime ().

Answer two:

tl; dr

The input string is in standard ISO 8601 format.

Instant. Parse (" the 2011-08-12 T20:17:46. 384 z ")

ISO 8601

This format is defined by the utility standard ISO 8601.

Separate the T date part from the time part. The Z peer device is UTC (that is, offset from -UTC zero hours - minutes - seconds).

java.time

The old date-time classes that came bundled with the earliest versions of Java proved poorly designed, confusing and cumbersome. Avoid them.

Instead, use the java.time framework built into Java 8 and later. The java.time class replaces the older date-time class and the very successful Joda-time library.

The java.time class defaults to ISO 8601 when parsing/generating textual representations of date and time values.

This Instant class represents the time in UTC on the timeline with a resolution of nanoseconds. This class can parse your input string directly without bothering to define a formatting mode.

Answer three:

There are other ways to parse it than the first answer. To parse it, do the following:

1. If you want to get information about dates and times, you can parse them as ZonedDatetime (since Java 8) or Date (old) objects:

// ZonedDateTime's default format requires a zone ID(like [Australia/Sydney]) in the end.
// Here, we provide a format which can parse the string correctly.
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
ZonedDateTime zdt = ZonedDateTime.parse("The 2011-08-12 T20:17:46. 384 z", dtf);
Copy the code

or

// 'T' is a literal.
// 'X' is ISO Zone Offset[like +01, -08]; For UTC, it is interpreted as 'Z'(Zero) literal.
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX";

// since no built-in format, we provides pattern directly.
DateFormat df = new SimpleDateFormat(pattern);

Date myDate = df.parse("The 2011-08-12 T20:17:46. 384 z");
Copy the code

2. If you don’t care about dates and times and just want to treat information as nanoseconds, you can use Instant:

// The ISO format without zone ID is Instant's default.
// There is no need to pass any format.
Instant ins = Instant.parse("The 2011-08-12 T20:17:46. 384 z");
Copy the code

The article translated from Stack Overflow: stackoverflow.com/questions/8…