Starting with JDK 8

Starting with JDK 8, a convenient solution to get the current local date-time in the default time zone is to call the ZonedDateTime.now() method:

ZonedDateTime zlt = ZonedDateTime.now();

So, this is the current date in the default time zone. Furthermore, this date should be displayed in all the available time zones that are obtained via the ZoneId class:

Set<String> zoneIds = ZoneId.getAvailableZoneIds();

Finally, the code can loop the zoneIds, and for each zone id, it can call the ZonedDateTime.withZoneSameInstant(ZoneId zone) method. This method returns a copy of this date-time with a different time zone, retaining the instant:

public static List<String> localTimeToAllTimeZones() {

List<String> result = new ArrayList<>();
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("yyyy-MMM-dd'T'HH:mm:ss a Z");
ZonedDateTime zlt = ZonedDateTime.now();

zoneIds.forEach((zoneId) -> {
result.add(zlt.format(formatter) + " in " + zoneId + " is "
+ zlt.withZoneSameInstant(ZoneId.of(zoneId))
.format(formatter));
});

return result;
}

An output snapshot of this method can be as follows:

2019-Feb-26T14:26:30 PM +0200 in Africa/Nairobi 
is 2019-Feb-26T15:26:30 PM +0300
2019-Feb-26T14:26:30 PM +0200 in America/Marigot
is 2019-Feb-26T08:26:30 AM -0400
...
2019-Feb-26T14:26:30 PM +0200 in Pacific/Samoa
is 2019-Feb-26T01:26:30 AM -1100

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.182.62