How do I make this unit test pass in all time zones independent of whether DST is active or not?
import static org.junit.Assert.*;
import java.text.SimpleDat
By default, a new SimpleDateFormat
will use the system default time zone. If you want a specific time zone, you should call setTimeZone
on it:
private final static SimpleDateFormat DATE_FORMAT = createFormat();
private static SimpleDateFormat createFormat() {
// Make sure there are no locale-specific nasties, either...
SimpleDateFormat ret = new SimpleDateFormat("yyyy.MM.dd'T'HH:mm.ss:SSSZZ",
Locale.US);
ret.setTimeZone(TimeZone.getTimeZone("Etc/UTC");
}
For your second test, you'd want to change it to:
new DateTime(0L, DateTimeZone.UTC);
Note that you shouldn't usually use a static SimpleDateFormat
variable, as it's not thread-safe. (Whereas the Joda Time DateTimeFormatter
implementation is thread-safe.)
Or you can compare the date and put
@Ignore("Timezone issues")
@Test
In your unit-test