我有這樣的方法:與日期時間解析器格式錯誤
public Date parseDate(String dateStr) {
try {
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
return sdfSource.parse(dateStr);
}
catch(Exception e) {
throw new RuntimeException("Error occurred while parsing date: " + dateStr);
}
}
而且我的單元測試如下:
public void testEDTDate() throws Exception {
DateFormatConverter converter = new DateFormatConverter();
Date date = converter.parseDate("2009-09-15 15:28:20.0 EDT");
System.out.println("Converted Date: " + date.toString());
}
輸出是:
Wed Sep 16 02:28:20 ICT 2009
這會對本機測試失敗。預期結果如下:
Tue Sep 15 15:28:20.0 EDT 2009
輸出的格式在錯過第二個格式時也錯誤。我應該如何修復以按預期顯示日期?
請檢查這個.. http://stackoverflow.com/questions/4203718/converting-string-to-date-with-timezone – 2012-04-24 15:00:40