我想将 Twitter 响应中的日期字符串转换为 Date 对象,但我总是收到 ParseException 并且我看不到错误!?!
输入字符串:2010 年 12 月 23 日星期四 18:26:07 +0000
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但这给出了相同的结果。
我在 Mac OS X 上使用 Java 1.6。
干杯,
和我
您的格式字符串对我有用,请参阅:
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")); }
输出:
更新
Roland Illig 是对的:SimpleDateFormat 是 Locale 相关的,所以只需使用明确的英文 Locale:
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);