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

3.7k views Asked by At

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?

4

There are 4 answers

0
Harshad Pansuriya On BEST ANSWER

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'");
0
Meno Hochschild On

The other answers are wrong with respect to time zone / offset - interpretation. The trailing "Z" denotes UTC+00:00 and must not be interpreted just as literal. The documentation of Android defines the suitable pattern symbol X which can handle this kind of input. If you don't care about this detail then you will not get an exception but wrong data (which is even worse).

So the final solution including the fix for the hour-part looks like:

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

Note: Use XX instead of XXX if you have offsets without colon.

0
Pushpendra On

Try this:

   String newsDate = "2017-01-02T01:41:24Z";    
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
   SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mma");

   try {
        texview.setText(sdf1.format(sdf.parse(newsDate)));
        } catch (ParseException e) {
          e.printStackTrace();
          System.out.println("bad pattern");
        }
0
Andrews Agyemang Opoku On

This works perfectly for me.

 String dateNew= "2017-01-02T01:41:24Z";

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


try {
    texview.setText(sdf1.format(sdf.parse(dateNew)));
    } catch (ParseException e) {
      e.printStackTrace();
      System.out.println("bad pattern");
    }