1

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)

Community
  • 1
  • 1
lhuber
  • 403
  • 1
  • 3
  • 13

3 Answers3

2

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);
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
2
 public static void main(String[] args) throws ParseException {
    String dateString = "Wed, 19 Oct 2016 12:00 PM CEST";
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:m a zzz", Locale.US);
    Date date = sdf.parse(dateString);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    System.out.println(calendar);
  }

The same thing worked for me.

Make sure all the imports are correct

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
Muzy
  • 505
  • 2
  • 6
  • 16
0

Your condition.getString("date") and the format in the "SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:m a z", Locale.US);" doesn't match.