ISO 8601 Android String to Date / Time Object

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

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

How can I get this in an object such as Time or Date? I initially want to output it in a different format, but later it will have to do other things (i.e., Maybe use it in a different format).

Hurrah

+46
java android parsing iso8601
Oct. 15 '10 at 10:24
source share
4 answers
 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 an existing message about this problem.

+118
Oct. 15 '10 at 10:44
source share

This question was asked in 2010, and then it was right that either SimpleDateFormat or Joda-Time would be the tools you should use. It's quite a while now. Use today

  Instant iStart = Instant.parse(dtStart); 

Yes, it is that simple. Your string is in ISO 8601 format, and classes from java.time , the modern date and time Java API, parse ISO 8601 without any explicit formatting tool. Instant is just one of these classes.

Question: Can I use java.time on Android?

Yes, java.time works great on old and new Android devices. This requires at least Java 6 .

  • In Java 8 and later, as well as on newer Android devices (from level 26 API, I said that) a modern built-in API.
  • In Java 6 and 7, get ThreeTen Backport, a backport of new classes (ThreeTen for JSR 310; see links below).
  • On (older) Android, use the Android version of ThreeTen Backport. It is called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with org.threeten.bp .

References

+6
May 23 '18 at 6:57
source share

You can use the Java SimpleDateFormat simple analysis method or use the JodaTime DateTimeFormat to create a DateTimeFormatter file and parse the DateTime object accordingly

+2
Oct. 15 '10 at
source share

Suppose you would like to calculate the time difference, given the values ​​separated by columns.
end β†’ 15:24:04
start β†’ 09:27:37
Without using Date and SimpleDateFormat, I did this:

 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"); 
-one
May 23 '18 at 5:19
source share



All Articles