Fix date format tests to not depend from time zone

This commit is contained in:
Alexey Andreev 2015-10-11 11:54:37 +03:00
parent 44173021f2
commit 0aa8f8bda7
2 changed files with 12 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone;
import org.junit.Test; import org.junit.Test;
/** /**
@ -29,6 +30,10 @@ import org.junit.Test;
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class DateFormatTest { public class DateFormatTest {
public DateFormatTest() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
}
@Test @Test
public void shortDateFormatHandled() throws ParseException { public void shortDateFormatHandled() throws ParseException {
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH); DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);

View File

@ -26,6 +26,10 @@ import org.junit.Test;
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class SimpleDateFormatTest { public class SimpleDateFormatTest {
public SimpleDateFormatTest() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
}
@Test @Test
public void firstDayOfWeekMatches() { public void firstDayOfWeekMatches() {
assertEquals(Calendar.SUNDAY, new GregorianCalendar(Locale.ENGLISH).getFirstDayOfWeek()); assertEquals(Calendar.SUNDAY, new GregorianCalendar(Locale.ENGLISH).getFirstDayOfWeek());
@ -135,15 +139,15 @@ public class SimpleDateFormatTest {
private Date getDateWithZoneOffset(long milliseconds) { private Date getDateWithZoneOffset(long milliseconds) {
Calendar calendar = new GregorianCalendar(Locale.ENGLISH); Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTimeInMillis(milliseconds); calendar.setTimeInMillis(milliseconds);
milliseconds -= calendar.get(Calendar.ZONE_OFFSET);
milliseconds -= calendar.get(Calendar.DST_OFFSET);
return new Date(milliseconds); return new Date(milliseconds);
} }
private long getTimeWithoutZoneOffset(Date date) { private long getTimeWithoutZoneOffset(Date date) {
Calendar calendar = new GregorianCalendar(Locale.ENGLISH); Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTime(date); calendar.setTime(date);
return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET); return calendar.getTimeInMillis();
} }
} }