×

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

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.