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
Release: 2024-10-29 12:41:29
Original
258 people have browsed it

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>
Copy after login

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!

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