Twitter 日期无法解析? [英] Twitter date unparseable?
问题描述
我想将 Twitter 响应中的日期字符串转换为 Date 对象,但我总是收到 ParseException 并且我看不到错误!?!
I want to convert the date string in a Twitter response to a Date object, but I always get a ParseException and I cannot see the error!?!
输入字符串:2010 年 12 月 23 日星期四 18:26:07 +0000
Input string: Thu Dec 23 18:26:07 +0000 2010
SimpleDateFormat
模式:
EEE MMM dd HH:mm:ss ZZZZZ yyyy
方法:
public static Date getTwitterDate(String date) {
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
Date twitterDate = null;
try {
twitterDate = sf.parse(date);
} catch (Exception e) {}
return twitterDate;
}
我也试过这个:http://friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZ 但这给出了相同的结果.
I also tried this: http://friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZ but that gives the same result.
我在 Mac OS X 上使用 Java 1.6.
I use Java 1.6 on Mac OS X.
干杯,
安迪
推荐答案
你的格式字符串对我有用,见:
Your format string works for me, see:
public static Date getTwitterDate(String date) throws ParseException {
final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
return sf.parse(date);
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010"));
}
输出:
格林威治标准时间 2010 年 12 月 3 日星期五 18:26:07
Fri Dec 03 18:26:07 GMT 2010
更新
Roland Illig 是对的:SimpleDateFormat 是 Locale 相关的,所以只需使用明确的英语语言环境:SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
Roland Illig is right: SimpleDateFormat is Locale dependent, so
just use an explicit english Locale:
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
这篇关于Twitter 日期无法解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!