Fixing Yet Another Date & Time Problem

This commit is contained in:
Ali Dehghani 2020-01-27 23:34:21 +03:30
parent 0648ba53ab
commit e9daf73125
1 changed files with 8 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import org.junit.Test;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.TimeZone;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -18,12 +19,19 @@ public class ConvertToOffsetDateTimeUnitTest {
@Test
public void givenDate_whenHasOffset_thenConvertWithOffset() {
TimeZone prevTimezone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Date date = new Date();
date.setHours(6);
date.setMinutes(30);
OffsetDateTime odt = ConvertToOffsetDateTime.convert(date, 3, 30);
assertEquals(10, odt.getHour());
assertEquals(0, odt.getMinute());
// Reset the timezone to its original value to prevent side effects
TimeZone.setDefault(prevTimezone);
}
}