Merge pull request #3084 from steinhauer-software/BAEL-1317

BAEl-1317: Display All Time Zones With GMT and UTC
This commit is contained in:
Loredana Crusoveanu 2017-11-21 21:49:16 +02:00 committed by GitHub
commit 67e78dbf76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,55 @@
package com.baeldung.timezonedisplay;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class TimezoneDisplay {
public enum OffsetBase {
GMT, UTC
}
public List<String> compileListFor(OffsetBase base) {
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
LocalDateTime now = LocalDateTime.now();
return availableZoneIds
.stream()
.map(ZoneId::of)
.sorted(new ZoneComparator())
.map(id -> String.format("(%s%s) %s", base, getOffset(now, id), id.getId()))
.collect(Collectors.toList());
}
private String getOffset(LocalDateTime dateTime, ZoneId id) {
return dateTime
.atZone(id)
.getOffset()
.getId()
.replace("Z", "+00:00");
}
private class ZoneComparator implements Comparator<ZoneId> {
@Override
public int compare(ZoneId zoneId1, ZoneId zoneId2) {
LocalDateTime now = LocalDateTime.now();
ZoneOffset offset1 = now
.atZone(zoneId1)
.getOffset();
ZoneOffset offset2 = now
.atZone(zoneId2)
.getOffset();
return offset1.compareTo(offset2);
}
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.timezonedisplay;
import java.util.List;
public class TimezoneDisplayApp {
public static void main(String... args) {
TimezoneDisplay display = new TimezoneDisplay();
System.out.println("Time zones in UTC:");
List<String> utc = display.compileListFor(TimezoneDisplay.OffsetBase.UTC);
utc.forEach(System.out::println);
System.out.println("Time zones in GMT:");
List<String> gmt = display.compileListFor(TimezoneDisplay.OffsetBase.GMT);
gmt.forEach(System.out::println);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.timezonedisplay;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class TimezoneDisplayJava7 {
public enum OffsetBase {
GMT, UTC
}
public List<String> compileListFor(TimezoneDisplayJava7.OffsetBase base) {
String[] availableZoneIds = TimeZone.getAvailableIDs();
List<String> result = new ArrayList<>(availableZoneIds.length);
for (String zoneId : availableZoneIds) {
TimeZone curTimeZone = TimeZone.getTimeZone(zoneId);
String offset = calculateOffset(curTimeZone.getRawOffset());
result.add(String.format("(%s%s) %s", base, offset, zoneId));
}
Collections.sort(result);
return result;
}
private String calculateOffset(int rawOffset) {
if (rawOffset == 0) {
return "+00:00";
}
long hours = TimeUnit.MILLISECONDS.toHours(rawOffset);
long minutes = TimeUnit.MILLISECONDS.toMinutes(rawOffset);
minutes = Math.abs(minutes - TimeUnit.HOURS.toMinutes(hours));
return String.format("%+03d:%02d", hours, Math.abs(minutes));
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.timezonedisplay;
import java.util.List;
public class TimezoneDisplayJava7App {
public static void main(String... args) {
TimezoneDisplayJava7 display = new TimezoneDisplayJava7();
System.out.println("Time zones in UTC:");
List<String> utc = display.compileListFor(TimezoneDisplayJava7.OffsetBase.UTC);
for (String timeZone : utc) {
System.out.println(timeZone);
}
System.out.println("Time zones in GMT:");
List<String> gmt = display.compileListFor(TimezoneDisplayJava7.OffsetBase.GMT);
for (String timeZone : gmt) {
System.out.println(timeZone);
}
}
}