Search code examples
javaandroidsimpledateformatparse-error

Android SimpleDateFormat Unparsable Date


i got this Date String for example:

Wed, 19 Oct 2016 12:00 PM CEST

now i am trying to convert it to a Calendar with the help of the SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:m a zzz", Locale.US);

And when i try to parse it i get the following error:

Unparseable date: "Wed, 19 Oct 2016 12:00 PM CEST" (at offset 26)

I appreciate every help!

Edit:

Full parsing code:

@Override
public WeatherData parseCurrentWeatherData(String jsonString) {

    WeatherData weatherData = new WeatherData();

    try {
        JSONObject obj = new JSONObject(jsonString);

        JSONObject mainObj = obj.getJSONObject("query").getJSONObject("results").getJSONObject("channel");

        JSONObject condition = mainObj.getJSONObject("item").getJSONObject("condition");

        Calendar cal = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:m a z", Locale.US);
        cal.setTime(sdf.parse(condition.getString("date")));

        weatherData.setCalendar(cal);

    } catch(JSONException | ParseException ex) {
        Log.e("DataFetcher", ex.getLocalizedMessage());
    }

    return weatherData;
}

Solution:

It looks like Android can't parse some timezones. Thanks to @Burhanuddin Rashid for this approach.

String strDate = condition.getString("date").replace("CEST", "GMT+0200");

Solution here: Unparseable date: "Fri Oct 10 23:11:07 IST 2014" (at offset 20)


Solution

  • Please add extra d in it like this :

        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:m a zzz", Locale.US);
        sdf.setLenient(true);