Richard Kuodis Richard Kuodis - 1 month ago 8
Android Question

Android Unparseable date (at offset 0)

I am trying to parse string date

"04 Nov 2016 16:30:07 +0300"


My formatter is
SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH)


And the code is
Date date = DATE_IN_FORMAT.parse(dateStr.trim());


I keep getting exception
java.text.ParseException: Unparseable date: "04 Nov 2016 16:30:07 +0300" (at offset 0)


I tried switching Locale to
Locale.US
and device default Locale, which did not solve my problem.

Answer

Try this

    String dateString = "04 Nov 2016 16:30:07 +0300";

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z");
    Date date;
    try {
        date = dateFormat.parse(dateString);

        dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        String formatedDate = dateFormat.format(date);

        Log.d("Date", formatedDate);

    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

Logged Output is

11-04 19:21:18.712: D/Date(11703): 2016-11-04 07:00
Comments