Search code examples
javaandroidandroid-studioandroid-date

Convert string with millisecond and timezone to Date


I have string in this format: 2017-04-06T09:29:12.225Z

I need it in date format i tried with:

   private Date convertStringToDate(String dateString) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssZ");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return convertedDate;
}

But it showing Unparsable error?


Solution

  • Try

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");