Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unparsable Date exception with SimpleDateFormat

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

like image 749
Atlas91 Avatar asked Jan 18 '26 13:01

Atlas91


1 Answers

The problem in zone, zzz expect name of timezone, e.g. EST, GMT or other.

The correct pattern for your case:

EEE MMM dd HH:mm:ss 'GMT'XXX yyyy
like image 53
alexey28 Avatar answered Jan 21 '26 03:01

alexey28



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!