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

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

Barbara Streisand
Barbara StreisandOriginal
2024-12-06 06:34:10717browse

How to Parse the Date String

Understanding Date Format: 2011-08-12T20:17:46.384Z

Unable to parse the date string "2011-08-12T20:17:46.384Z" using DateFormat.getDateInstance().parse(dateStr), the question arises regarding the unrecognized format.

Decoding the Format

The given date format, "2011-08-12T20:17:46.384Z", consists of the following elements:

  • "T": A literal separator between the date and time components.
  • "Z": Represents "zero hour offset," also known as "Zulu time" (UTC).

Creating a SimpleDateFormat

To successfully parse the date string, SimpleDateFormat can be used. Below is the code to create a SimpleDateFormat object that can handle the given format:

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

Using Joda Time

Alternatively, Joda Time provides a convenient way to parse the date string:

DateTimeFormat.dateTime().parseDateTime("2011-08-12T20:17:46.384Z");

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

Statement:
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