Skip to content

Commit 441de07

Browse files
Saikatpivovarit
authored andcommitted
[BAEL-1902] Set timezone of a date (eugenp#4659)
* Convert time in preferred time zone using Java 7, 8 and Joda-time * Update the code as per review comments * Incorporate review comments * Incorporate review changes * Use snippets from unit test only
1 parent 0681d23 commit 441de07

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.datetime;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.time.Instant;
5+
import java.time.ZoneId;
6+
import java.time.ZonedDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
import java.util.Calendar;
9+
import java.util.Date;
10+
import java.util.TimeZone;
11+
12+
import org.joda.time.DateTime;
13+
import org.joda.time.DateTimeZone;
14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
17+
public class UseTimeZoneUnitTest {
18+
19+
/* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */
20+
21+
String timeZone = "Asia/Singapore";
22+
23+
private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a";
24+
25+
@Test
26+
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() {
27+
Date nowUtc = new Date();
28+
TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone);
29+
30+
Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore);
31+
nowAsiaSingapore.setTime(nowUtc);
32+
33+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
34+
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
35+
36+
System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone()
37+
.getID(), simpleDateFormat.format(nowAsiaSingapore.getTime())));
38+
39+
Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond());
40+
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone());
41+
42+
}
43+
44+
@Test
45+
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() {
46+
Instant nowUtc = Instant.now();
47+
ZoneId asiaSingapore = ZoneId.of(timeZone);
48+
49+
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
50+
51+
System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
52+
nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN))));
53+
54+
Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond());
55+
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
56+
}
57+
58+
@Test
59+
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() {
60+
org.joda.time.Instant nowUtc = org.joda.time.Instant.now();
61+
DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone);
62+
63+
DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore);
64+
65+
System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
66+
nowAsiaSingapore.toString(PATTERN)));
67+
68+
Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis());
69+
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
70+
}
71+
72+
}

0 commit comments

Comments
 (0)