Skip to content

Commit 67e78db

Browse files
authored
Merge pull request eugenp#3084 from steinhauer-software/BAEL-1317
BAEl-1317: Display All Time Zones With GMT and UTC
2 parents 8560a94 + 1256713 commit 67e78db

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.timezonedisplay;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.ZoneId;
5+
import java.time.ZoneOffset;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.stream.Collectors;
10+
11+
public class TimezoneDisplay {
12+
13+
public enum OffsetBase {
14+
GMT, UTC
15+
}
16+
17+
public List<String> compileListFor(OffsetBase base) {
18+
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
19+
20+
LocalDateTime now = LocalDateTime.now();
21+
return availableZoneIds
22+
.stream()
23+
.map(ZoneId::of)
24+
.sorted(new ZoneComparator())
25+
.map(id -> String.format("(%s%s) %s", base, getOffset(now, id), id.getId()))
26+
.collect(Collectors.toList());
27+
}
28+
29+
private String getOffset(LocalDateTime dateTime, ZoneId id) {
30+
return dateTime
31+
.atZone(id)
32+
.getOffset()
33+
.getId()
34+
.replace("Z", "+00:00");
35+
}
36+
37+
private class ZoneComparator implements Comparator<ZoneId> {
38+
39+
@Override
40+
public int compare(ZoneId zoneId1, ZoneId zoneId2) {
41+
LocalDateTime now = LocalDateTime.now();
42+
43+
ZoneOffset offset1 = now
44+
.atZone(zoneId1)
45+
.getOffset();
46+
47+
ZoneOffset offset2 = now
48+
.atZone(zoneId2)
49+
.getOffset();
50+
51+
return offset1.compareTo(offset2);
52+
}
53+
}
54+
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.timezonedisplay;
2+
3+
import java.util.List;
4+
5+
public class TimezoneDisplayApp {
6+
7+
public static void main(String... args) {
8+
TimezoneDisplay display = new TimezoneDisplay();
9+
10+
System.out.println("Time zones in UTC:");
11+
List<String> utc = display.compileListFor(TimezoneDisplay.OffsetBase.UTC);
12+
utc.forEach(System.out::println);
13+
14+
System.out.println("Time zones in GMT:");
15+
List<String> gmt = display.compileListFor(TimezoneDisplay.OffsetBase.GMT);
16+
gmt.forEach(System.out::println);
17+
}
18+
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.timezonedisplay;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.TimeZone;
7+
import java.util.concurrent.TimeUnit;
8+
9+
public class TimezoneDisplayJava7 {
10+
11+
public enum OffsetBase {
12+
GMT, UTC
13+
}
14+
15+
public List<String> compileListFor(TimezoneDisplayJava7.OffsetBase base) {
16+
String[] availableZoneIds = TimeZone.getAvailableIDs();
17+
List<String> result = new ArrayList<>(availableZoneIds.length);
18+
19+
for (String zoneId : availableZoneIds) {
20+
TimeZone curTimeZone = TimeZone.getTimeZone(zoneId);
21+
22+
String offset = calculateOffset(curTimeZone.getRawOffset());
23+
24+
result.add(String.format("(%s%s) %s", base, offset, zoneId));
25+
}
26+
27+
Collections.sort(result);
28+
29+
return result;
30+
}
31+
32+
private String calculateOffset(int rawOffset) {
33+
if (rawOffset == 0) {
34+
return "+00:00";
35+
}
36+
long hours = TimeUnit.MILLISECONDS.toHours(rawOffset);
37+
long minutes = TimeUnit.MILLISECONDS.toMinutes(rawOffset);
38+
minutes = Math.abs(minutes - TimeUnit.HOURS.toMinutes(hours));
39+
40+
return String.format("%+03d:%02d", hours, Math.abs(minutes));
41+
}
42+
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.timezonedisplay;
2+
3+
import java.util.List;
4+
5+
public class TimezoneDisplayJava7App {
6+
7+
public static void main(String... args) {
8+
TimezoneDisplayJava7 display = new TimezoneDisplayJava7();
9+
10+
System.out.println("Time zones in UTC:");
11+
List<String> utc = display.compileListFor(TimezoneDisplayJava7.OffsetBase.UTC);
12+
for (String timeZone : utc) {
13+
System.out.println(timeZone);
14+
}
15+
16+
System.out.println("Time zones in GMT:");
17+
List<String> gmt = display.compileListFor(TimezoneDisplayJava7.OffsetBase.GMT);
18+
for (String timeZone : gmt) {
19+
System.out.println(timeZone);
20+
}
21+
}
22+
23+
}

0 commit comments

Comments
 (0)