3

The following Java test passes on our US hosted build server. It also passes on non-US servers, e.g. in Germany. It fails on my local server, which is running in Ireland. The following code illustrates a failing test.

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

Is there a system setting I can provide to get these tests passing locally?

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);
}  
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Damo
  • 1,449
  • 3
  • 16
  • 29
  • 1
    Uhm ... what are you testing? No code of yours is under test here. Is this test just for verification of how the Java classes work? – Joachim Sauer Jul 27 '12 at 08:25
  • Assert.assertEquals("4/6/09 11:30 AM", formattedOutput);..... – Damo Jul 27 '12 at 09:04
  • The code under test is simply inline for readability. These lines represent the code under test: SimpleDateFormat dateFormatter = new SimpleDateFormat(); final String formattedOutput = dateFormatter.format(utilDate); – Damo Jul 27 '12 at 09:05
  • Well, then there's a bug in your code under test: if you need a *specific* format, then you should actually *provide* that format (in the `SimpleDateFormat` constructor) and not just assume that your system is configured to provide exactly the format you need. – Joachim Sauer Jul 27 '12 at 09:22

2 Answers2

4

Have to tried to provide a pattern to the SimpleDateFormat ?

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

Olivier.Roger
  • 4,241
  • 5
  • 40
  • 68
  • That will get the test to pass yes. I would rather not have to change the code or the test. This test does work for my fellow developers in Germany - as well as the US. – Damo Jul 27 '12 at 09:03
  • You should be able to set the JVM local using: `-Duser.language=xz -Duser.country=XZ` as indicated in http://stackoverflow.com/questions/64038/setting-java-locale-settings – Olivier.Roger Jul 27 '12 at 09:05
  • 1
    I found the cleanest solution was to update my cygwin env, with the command export LC_ALL=en_US.UTF-8 - thanks for the solution – Damo Jul 30 '12 at 10:09
4

The time is correct but the SimpleDateFormat() constructor internally calls a package private construtor using Locale.getDefault(). Thus you either can provide a format of your own or provide another locale, which seems to only be possible with a custom format, i.e. using SimpleDateFormat(String pattern, Locale locale).

The problem is that SimpleDateFormat() uses a locale dependent pattern, thus the system's default locale might result in a different pattern than what you get in the USA (I assume the German server doesn't use the German locale as its default since then you should get a date like 06.04.09 11:30).

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • "system's default locale" - so yea, that is what I had thought. I updated my windows Date format from dd/MM/yyyy to MM/dd/yyyy - but this made no difference. – Damo Jul 27 '12 at 09:01
  • @Damo did you change the format in the OS? Note that AFAIK this doesn't have effects on Java. The locale specific formats are normally embedded into the Java libraries. But Java chooses the locale that is provided by the system as its default. You should be able to change the locale that is used by using the following system properties: `-Duser.language=en -Duser.country=IE` – Thomas Jul 27 '12 at 09:41
  • I was able to update the system locale using 'export LC_ALL=en_US.UTF-8' - thanks – Damo Jul 30 '12 at 10:13