【发布时间】:2017-02-28 23:09:49
【问题描述】:
我得到了这个日期字符串,例如:
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)
【问题讨论】:
-
您的代码运行良好。
-
但是为什么我的环境会出现解析错误呢?
-
你能发布你的完整解析代码吗?
-
我更新了我的问题@0riginal
-
试试打印 condition.getString("date") 来检查你是否得到了正确的字符串
标签: java android simpledateformat parse-error