我一直在开发一个提要阅读器应用程序,我需要从 RSS 和 Atom 解析 de pubDate。根据 RSS 协议,pubDate 应该是 RFC 822 格式,但是我发现一些具有不同日期格式的 RSS 发布者,这是我目前这样做的方式,不确定这是否是最好的方式,建议表示赞赏:
相关代码在这里:
//List that keeps the data formats allowed
private static List<String> formats;
static {
formats = Arrays.asList(
//RFC 822 possible formats
"EEE, d MMM yyyy HH:mm:ss zzz", //Sun, 19 May 2002 15:21:36 GMT
"EEE, d MMM yyyy HH:mm zzz", // Sun, 19 May 2002 15:21 GMT
"EEE, d MMM yyyy HH:mm:ss", // Sun, 19 May 2002 15:21:36
"EEE, d MMM yyyy HH:mm", // Sun, 19 May 2002 15:21
"d MMM yyyy HH:mm:ss zzz", // 19 May 2002 15:21:36 GMT
"d MMM yyyy HH:mm zzz", // 19 May 2002 15:21 GMT
"d MMM yyyy HH:mm:ss", // 19 May 2002 15:21:36
"d MMM yyyy HH:mm", // 19 May 2002 15:21
//RFC 8339
"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ", // 1996-12-19T16:39:57-0800
"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZZZ", // 1937-01-01T12:00:27.87+0020
"yyyy'-'MM'-'dd'T'HH':'mm':'ss", // 1937-01-01T12:00:27
//ISO 8601 same as RFC8339 but it allows to ommit the 'T' and replace it with a ' '.
"yyyy'-'MM'-'dd' 'HH':'mm':'ssZZZ", // 1996-12-19 16:39:57-0800
"yyyy'-'MM'-'dd' 'HH':'mm':'ss.SSSZZZ", // 1937-01-01 12:00:27.87+0020
"yyyy'-'MM'-'dd' 'HH':'mm':'ss" // 1937-01-01 12:00:27
);
}
/*
Parses a string with a date and returns the milliseconds. The RSS standard says that dates are
formatted according to RFC 822 however some are in RFC 3339 or ISO6091 format, so a brute force
approach is made to parse the date, if it fails null is returned;
*/
public static Long getDateFromString( String date){
for(String format: formats){
//Creates SimpleDateFormat with US Locale as recommend in google docs to parse the input
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
try{
return sdf.parse(date).getTime();
}catch (ParseException ex){
Log.i("Utils","Wrong date format: inputDate: " +date + " format: " + format);
ex.printStackTrace();
}
}
return null;
}
它在 Android 上,因此不能选择使用 Joda-Time,因为据我所知,它的初始化速度很慢,而且它是一个庞大的库。我也知道 SimpleDateFormat 不是线程安全的,但在我的情况下这并不重要。
提前致谢。