Display List of TimeZone with GMT and UTC in Java
It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT.
TimeZone
Those who observe the same standard time for work and leisure are said to live in a time zone. Since it is easy for regions with regular connection to retain the same time, time zones frequently follow the boundaries between nations and their subdivisions rather than rigidly following longitude. The Java library includes the following time zones:
Java time zones for cities in Africa (total 54 time zones are available)
Time zones in Java for American cities (total 167 time zones are available)
Antarctica time zones in Java (total 12 time zones are available)
Arctic Java timezones (total 1 time zones are available)
Java time zones for cities and regions in Asia (total 99 time zones are available)
Time zones in Java for Australian cities and regions (total 12 time zones are available)
Time zones in Java for Australian cities and regions (total 23 time zones are available)
Brazilian timezones for cities and regions (There are a total of 4 time zones available)
Other areas include: Canada, Chile, Cuba, Egypt, Europe, and the US (total 257 time zones are available)
GMT
Greenwich Mean Time is referred to as GMT. At the Royal Observatory in Greenwich, London, the Shepherd Gate Clock displays the mean solar time, which is calculated starting at midnight. It is used to clearly identify epoch by avoiding the confusing mention of local time zone.
When the sun is directly above the Prime Meridian and at its highest point at Greenwich, it is precisely 12:00 p.m.
// java program to demonstrate display list of time zone with GMT
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class TZE
{
public static void main (String args[])
{
String[]id = TZ.getAvailableID ();
for (String id:id)
{
System.out.println (dTZ (TZ.gTZ (id)));
}
System.out.println ("\nTotal TZ ID " + id.length);
}
private static String displayTZ (TZ tz)
{
long hours = TimeUnit.MILLISECONDS.toHours (tz.getRawOffset ());
long minutes = TimeUnit.MILLISECONDS.toMinutes (tz.getRawOffset ())
- TimeUnit.HOURS.toMinutes (hours);
// avoid -4:-30 issue
min = Math.abs (min);
String res = "";
if (hours > 0)
{
res =
String.format ("(GMT+%d:%02d) %s", hours, min, tz.getID ());
}
else
{
res =
String.format ("(GMT%d:%02d) %s", hours, min, tz.getID ());
}
return res;
}
}
Output:

For display, the list is too long. With GMT, it shows all 629 time zones.
Coordinated Universal Time (UTC)
The world regulates clocks and time using the UTC time zone, which is a standard time zone. For Daylight Saving Time, it is a continuous time scale that does not alter (DST).
Greenwich Mean Time (GMT), which is the same worldwide, has been offered as an option by numerous overseas listeners. A 24-hour clock is used by GMT, and it is synchronised with the time at the Royal Observatory in Greenwich, England.
For example, if a news includes Eastern Daylight Time and GMT, it would be reported as "5 p.m. EDT (2100 GMT)." The Associated Press sometimes includes the local time and GMT in overseas reports, and when they do, it is placed in parentheses after the local time.
// java program
import java.util.Collections;
import java.util.List;
public class UtcE
{
public static void main(String args[])
{
List tz7 = DT.fetchTZ7(DT.OffsetType.UTC);
List tz8 = DT.fetchTZ8(DT.OffsetType.UTC);
Collections.sort(tz8);
tz8.forEach(System.out::println);
}
}
//
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public final class DateTimes
{
public enumOsT
{
GMT, UTC
}
private DT()
{
throw new AssertionError("Cannot be instantiated");
}
public static List<String> fetchTZ8(OffsetType type)
{
List<String>tz = new ArrayList<>();
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
LDT now = LDT.now();
zoneIds.forEach((zoneId) ->
{
tz.add("(" + type + now.atZone(ZoneId.of(zoneId))
.getOffset().getId().replace("Z", "+00:00") + ") " + zoneId);
});
return tz;
}
public static List<String> fetchTZ7(OffsetType type)
{
List<String>tz = new ArrayList<>();
String[] zoneIds = TZ.getAvailableIDs();
long timestamp = new Date().getTime();
for (String zoneId : zoneIds) {
TZ curTZ = TZ.getTZ(zoneId);
curTZ.useDaylightTime();
String offset = formatOffset(curTZ.getOffset(timestamp));
tz.add("(" + type + offset + ") " + zoneId);
}
return tz;
}
public static String formatOffset(int offset)
{
if (offset == 0)
{
return "+00:00";
}
long offsetIH = TimeUnit.MILLISECONDS.toHours(offset);
long offsetInMFH = TimeUnit.HOURS.toMinutes(offsetInHours);
long offsetIM = TimeUnit.MILLISECONDS.toMinutes(offset);
offsetIM = Math.abs(offsetIMFH - offsetIM);
return String.format("%+03d:%02d", offsetIH, offsetIM);
}
}
Output:
