Search code examples
androiddatesimpledateformatunparseable

Unparseable date: "2017-01-02T01:41:24Z" (at offset 10)


2017-01-02T01:41:24Z is my actual date format and I want to convert this date in yyyy-MM-dd hh:mma format.

Please see the following code I tried so far,

  String newsDate = "2017-01-02T01:41:24Z";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
        Date date = null;
        try
        {
            date = sdf.parse(newsDate);
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }

but at date = sdf.parse(newsDate); line I'm getting the following error:

"Unparseable date: "2017-01-02T01:41:24Z" (at offset 10)".

Please guide me, where could i have gone wrong?


Solution

  • Because you are using different Date Format which is not correct.

    Change this

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
    

    to this

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