ISO 8601 String to Date/Time object in Android

JavaAndroidParsingIso8601

Java Problem Overview


I have a string in standard ISO 8601 format that contains the date/time returned from a web service like so:

String dtStart = "2010-10-15T09:27:37Z"

How do I get this into an object such as Time or Date? I initially want to output it in a different format, but will need to do other stuff with it later (i.e. maybe use in a different format).

Cheers

Java Solutions


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

This is what you are looking for. There is existing post about this problem.

Solution 2 - Java

This question was asked in 2010, and back then it was correct that either SimpleDateFormat or Joda-Time would be the tools you should use. It’s quite a while ago now. Today use

    Instant iStart = Instant.parse(dtStart);

Yes, it’s this simple. Your string is in ISO 8601 format, and the classes from java.time, the modern Java date and time API, parse ISO 8601 without any explicit formatter. Instant is just one of those classes.

Edit: Question: requires android API 26 - what about supporting older versions?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Solution 3 - Java

You can use Java's SimpleDateFormat parse method or use JodaTime's DateTimeFormat to create a DateTimeFormatter and parse to a DateTime object accordingly

Solution 4 - Java

If you use current java.time or threetenbp simply parse it like this:

ZonedDateTime dt = ZonedDateTime.parse(dtStart);

Now you can access date and time values such as year, month, hour, etc.

Solution 5 - Java

Suppose you would like to calculate time difference given the column separated values.
finish --> 15:24:04
start --> 09:27:37
Without using Date and SimpleDateFormat, I did this way:

String tStart = "09:27:37";
String tFinish = "15:24:04";

String[] sTimeHourMinSec = tStart.split(":");
int sHour = Integer.valueOf(sTimeHourMinSec[0]);
int sMin = Integer.valueOf(sTimeHourMinSec[1]);
int sSec = Integer.valueOf(sTimeHourMinSec[2]);

String[] fTimeHourMinSec = tFinish.split(":");
int fHour = Integer.valueOf(fTimeHourMinSec[0]);
int fMin = Integer.valueOf(fTimeHourMinSec[1]);
int fSec = Integer.valueOf(fTimeHourMinSec[2]);

int diffTotSec = (fHour - sHour) * 3600 + (fMin - sMin) * 60 + (fSec - sSec);
int diffHours = diffTotSec / 3600;
int diffMins = (diffTotSec % 3600) / 60;
int diffSecs = (diffTotSec % 3600) % 60;

System.out.println("Difference: " + diffHours + " h " + diffMins + " m " + diffSecs + " sec");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneildeadmanView Question on Stackoverflow
Solution 1 - JavaSeitaridisView Answer on Stackoverflow
Solution 2 - JavaOle V.V.View Answer on Stackoverflow
Solution 3 - JavaJeroen RosenbergView Answer on Stackoverflow
Solution 4 - JavapramView Answer on Stackoverflow
Solution 5 - JavaSan AskarulyView Answer on Stackoverflow