我讨厌发布另一个 SimpleDateFormat 问题,但在 Google 或其他线程中找不到我的答案。我的代码在下面,评论指出了问题。
// The dateString format is "Thu Jul 19 00:04:11 +0000 2012". I confirmed that there
// is no preceding or trailing white space.
public static GregorianCalendar stringToDate(String dateString) {
GregorianCalendar calendar = new GregorianCalendar();
ParsePosition pos = new ParsePosition(0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d H:mm:ss Z yyyy");
Date date = simpleDateFormat.parse(dateString, pos);
// 'Unknown Source' error thrown by setTime() as date is ultimately null.
calendar.setTime(date);
return calendar;
}
我已经确认我的 dateString 参数正确通过,我的位置对象从 0 开始,并且我已经根据http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat多次查看 SimpleDateFormat .html。它看起来对我来说是正确的,但我仍然测试了“d”和“H”的各种排列。我唯一能想到的是,使用这个我缺少一些细微差别,或者我离基地很远。
这会编译(javac 1.6.0_33),但是当运行(java 版本“1.6.0_33”)时,它甚至不会处理传递给它的第一个 dateString 值。我正在运行 Windows 7 64 位日文版。
非常感谢任何帮助。