0

我知道对于我将在此处描述的问题,这是非常知名且简单的解决方案。我正在尝试解析以下日期格式,但即使传递正确的格式来解析该字符串,也只会获得解析异常。

try{
  String mDateFormat = "Tue Nov 03 13:46:28 GST 2015";
  SimpleDateFormat mFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
  Date mFormattedDate = mFormat.parse(mDateFormat);

  }catch(ParseException e){
     e.printStackTrace();
}

此字符串中包含的时区只会引发崩溃。如果我将其删除,它能够正确解析它。所以时区格式有问题,但即使我尝试了此链接声明的标准: SimpleDateFormat By Developer Site

有人建议我这里有什么问题吗?

谢谢,

4

3 回答 3

0

From all the experiments I did so far, it seems that the Android DateFormat parser implementation on which you are seeing a problem does not understand the GST zone. The simplest solution would be to use the formatter explicitly by replacing the GST to to GMT+4 as:

String mDateFormat = "Tue Nov 03 13:46:28 GMT+4 2015";

With this date string, Android seems to be returning the same conversion as the Desktop equivalent.

P.S (removing the TimeZone from the formatter wont be a correct solution since the conversions wont be accurate. Since you are getting this date from the Twitter API, you may need to filter the un-acceptible strings before formating on Android you can use:

mDateFormat.replace("GST", "GMT+4") 

before applyeing the DateFormat to the data.

于 2015-11-03T20:58:50.730 回答
-1

如果您使用的是英文日期,则将代码更改为

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
于 2015-11-03T15:58:35.013 回答
-1

我做了什么简单的解决方案:

1) 将时区从以下格式中分离为字符串并替换为 "" :

String mDateFormat = "Tue Nov 03 13:46:28 GST 2015";
String mTimeZone = mDateFormat.substring(20,23);
String mActualDate = mDateFormat.replace(mTimeZone + " ", "");

2)在应用 SimpleDateFormat 时获取默认时区并设置相同的时区

final String TWITTER = "EEE MMM dd HH:mm:ss yyyy";
SimpleDateFormat mSf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
mSf.setTimeZone(TimeZone.getDefault());
Date mNewDate = sf.parse(mActualDate);

3)获取具有相同时区的更新日期。无需使用 GMT 的东西进行转换。

于 2015-11-17T04:57:19.500 回答