Home >Java >javaTutorial >How to Parse ISO 8601 Date/Time Strings in Android?

How to Parse ISO 8601 Date/Time Strings in Android?

Linda Hamilton
Linda HamiltonOriginal
2024-10-29 12:41:29374browse

How to Parse ISO 8601 Date/Time Strings in Android?

Parsing ISO 8601 Date/Time Strings in Android

Question:

You have received a standard ISO 8601 string from a web service, such as "2010-10-15T09:27:37Z". How can you convert this string into a Date/Time object in Android for further manipulation?

Answer:

Android provides a SimpleDateFormat class that allows you to parse and format date/time strings. Here's how you can use it to convert an ISO 8601 string to a Date object:

<code class="java">String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
    Date date = format.parse(dtStart);
    System.out.println(date);  
} catch (ParseException e) {
    e.printStackTrace();
}</code>

This code uses the SimpleDateFormat object with a pattern that matches the ISO 8601 format of the string. The parse() method then converts the string into a Date object, which you can use for further processing.

The above is the detailed content of How to Parse ISO 8601 Date/Time Strings in Android?. 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