Merge pull request #7950 from kertpjatkin/master

[BAEL-3302] How to set the JVM Timezone
This commit is contained in:
Jonathan Cook 2019-11-25 20:40:47 +01:00 committed by GitHub
commit 2b256270fe
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package com.baeldung.timezone;
import org.junit.Test;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import static org.junit.Assert.assertEquals;
public class ModifyDefaultTimezoneUnitTest {
@Test
public void givenDefaultTimezoneSet_thenDateTimezoneIsCorrect() {
TimeZone.setDefault(TimeZone.getTimeZone("Portugal"));
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
assertEquals(calendar.getTimeZone(), TimeZone.getTimeZone("Portugal"));
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.timezone;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import static org.junit.Assert.assertEquals;
public class ModifyTimezonePropertyUnitTest {
@Before
public void setup() {
System.setProperty("user.timezone", "IST");
TimeZone.setDefault(null);
}
@After
public void teardown() {
System.clearProperty("user.timezone");
}
@Test
public void givenTimezonePropertySet_thenDateTimezoneIsCorrect() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
assertEquals(calendar.getTimeZone(), TimeZone.getTimeZone("IST"));
}
}