×

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:

Display List of TimeZone with GMT and UTC in Java

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:

Display List of TimeZone with GMT and UTC in Java

Related Topics

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Java Architecture

Java architecture is a combination of three parts they are JVM, JRE and JDK. These components will help in the functioning of the java programs. The process of code interpretation...

6 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.