"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Parse ISO 8601 Date/Time Strings in Android?

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

Published on 2024-12-20
Browse:230

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:

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();
}

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3