I'm trying to get milliseconds from a date but I got the exception
java.text.ParseException: Unparseable date: "Thu Jul 25 10:56:29 GMT+02:00 2019"
That's what I've done so far:
I took the date from a DatePicker with this pattern "EEE MMM dd HH:mm:ss zzz yyyy" and then passed to a method to get the milliseconds:
long milliDate = parseIso8601(dateTimeCalendar.getTime().toString());
private static long parseIso8601(String value) {
try {
return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(value).getTime();
} catch (ParseException ignored) {
return 0;
}
}
I got the exception and it returns 0. I don't know what's wrong. Thanks
SimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to usejava.time
, the modern Java date and time API. It is so much nicer to work with. – Ole V.V.Date
object. There’s no reason to convert it to a string and parse that string back into aDate
. As another aside your method nameparseIso8601
is misleading. The format you are parsing has no similarity to ISO 8601. – Ole V.V.