×

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 Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

How to Convert Decimal to Binary in Java

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

2 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.