I have an ISO 8601 time string and want to calculate the time difference to now in minutes without using joda time. How is this done?
Thanks
Parse it using SimpleDateFormat
to get a Date
, get the milliseconds-since-unix-epoch of that using Date.getTime()
, then compare with System.currentTimeMillis()
.
If your ISO-8601 strings contain a time zone offset as something like "-08:00" (which they certainly can), you'll need to remove the colon first, and use the Z
format specifier in SimpleDateFormat
. (In Java 7 you could use X
, but that's not available in Android's version of SimpleDateFormat
as far as I'm aware.)
What about javax.xml.datatype.Duration as returned by javax.xml.datatype.DatatypeFactory?