Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot parse date string coming from MongoDB in Android/Java

I am trying to parse date (2015-06-25T00:00:00.000Z) coming from MongoDB.

try {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
    Date date = format.parse("2015-06-25T00:00:00.000Z");
    return new Date().after(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Where am I wrong?

like image 508
Burak Avatar asked Dec 15 '22 07:12

Burak


1 Answers

Your pattern accepts only second digits but your string has fractional seconds as well

yyyy-MM-dd'T'HH:mm:ss    'Z'
2015-06-25 T 00:00:00.000 Z

You need to use

"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 
like image 80
Dmitri Timofti Avatar answered Dec 16 '22 20:12

Dmitri Timofti