日期格式问题

时间:2012-07-27 08:16:17

标签: java date timezone format

以下Java测试在我们的美国托管构建服务器上传递。它还传递非美国服务器,例如在德国。它在我在爱尔兰运行的本地服务器上失败。以下代码说明了一个失败的测试。

org.junit.ComparisonFailure: expected:<[4/6/09 11:30 AM]> but was:<[06/04/09 11:30]>

我可以提供系统设置来让这些测试在本地传递吗?

public void testFormattedDate() {
// Set the default time zone in case this unit test is executed in a different country
TimeZone.setDefault(TimeZone.getTimeZone(DateUtil.DEFAULT_TIMEZONE));
final Date utilDate = new Date();
utilDate.setDate(6);
utilDate.setHours(11);
utilDate.setMinutes(30);
utilDate.setMonth(3);
utilDate.setSeconds(45);
utilDate.setYear(109);

SimpleDateFormat dateFormatter = new SimpleDateFormat();        
final String formattedOutput = dateFormatter.format(utilDate);

Assert.assertEquals("4/6/09 11:30 AM", formattedOutput);
}  

1 个答案:

答案 0 :(得分:4)

必须尝试为SimpleDateFormat提供模式吗?

SimpleDateFormat dateFormatter = new SimpleDateFormat("d/M/yy HH:mm a");

相关问题