Home > Java > javaTutorial > How to Parse the Date String '2011-08-12T20:17:46.384Z' in Java?

How to Parse the Date String '2011-08-12T20:17:46.384Z' in Java?

Patricia Arquette
Release: 2024-12-08 03:16:08
Original
976 people have browsed it

How to Parse the Date String

Decoding the Timestamp String: 2011-08-12T20:17:46.384Z

In an attempt to parse the date string "2011-08-12T20:17:46.384Z" using Java's DateFormat.getDateInstance(), you encountered the error: "Unparseable date: '2011-08-12T20:17:46.384Z'". This indicates that the provided date format is not recognized by the method.

To successfully parse this date, you need to identify the proper format string. The "T" in the string separates the date from the time, and the "Z" denotes "Zulu time," which represents UTC (Coordinated Universal Time).

Parsing the Date Using SimpleDateFormat

To parse the date using SimpleDateFormat, you can employ the following code:

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

This format string explicitly specifies the date and time components, including the separator "T" and the Zulu time indicator "Z". Using this format, you can parse the date string as follows:

Date date = format.parse("2011-08-12T20:17:46.384Z");
Copy after login

Parsing the Date Using Joda Time

Alternatively, you can use Joda Time's ISODateTimeFormat.dateTime() to parse the date:

DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime("2011-08-12T20:17:46.384Z");
Copy after login

The above is the detailed content of How to Parse the Date String '2011-08-12T20:17:46.384Z' in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template