我有这个方法:
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
缺少第二个时,输出格式也错误。我应该如何修复以按预期显示日期?