以下 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);
}
最佳答案
必须尝试为 SimpleDateFormat 提供模式?
SimpleDateFormat dateFormatter = new SimpleDateFormat("d/M/yy HH:mm a");
关于java - 日期格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11683998/