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!?!
输入string:Thu Dec 23 18:26:07 +0000 2010
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.
干杯,
Andi
推荐答案
您的格式字符串适用于我,请参阅:
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"));
}
输出:
Fri Dec 03 18:26:07 GMT 2010
Fri Dec 03 18:26:07 GMT 2010
更新
Roland Illig是对的:SimpleDateFormat是依赖于区域设置,所以
只是使用一个明确的英语语言环境:
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屋!