Android SimpleDateFormat 无法解析的日期

Android SimpleDateFormat Unparsable Date

我得到了这个日期字符串,例如:

Wed, 19 Oct 2016 12:00 PM CEST

现在我正尝试借助 SimpleDateFormat

将其转换为日历
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:m a zzz", Locale.US);

当我尝试解析它时出现以下错误:

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

感谢您的帮助!

编辑:

完整解析代码:

@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;
}

解法:

看起来 Android 无法解析某些时区。感谢@Burhanuddin Rashid 的这种方法。

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

解决方法在这里:Unparseable date: "Fri Oct 10 23:11:07 IST 2014" (at offset 20)

请像这样在其中添加额外的 d :

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:m a zzz", Locale.US);
    sdf.setLenient(true);
 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);
  }

同样的事情对我有用。

确保所有导入都是正确的

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

你的 condition.getString("date") 和 "SimpleDateFormat sdf = new SimpleDateFormat("EEE 中的格式,dd MMM yyyy hh:m a z", Locale.US);"不匹配。