Search code examples
javacalendartimezonetimezone-offsetgregorian-calendar

How to get timezone from timezone offset in java?


I know how to get the opposite. That is given a timezone I can get the timezone offset by the following code snippet:

TimeZone tz = TimeZone.getDefault();
System.out.println(tz.getOffset(System.currentTimeMillis()));

I want to know how to get the timezone name from timezone offset.

Given,

timezone offset = 21600000 (in milliseconds; +6.00 offset)

I want to get result any of the following possible timezone names:

(GMT+6:00) Antarctica/Vostok
(GMT+6:00) Asia/Almaty
(GMT+6:00) Asia/Bishkek
(GMT+6:00) Asia/Dacca
(GMT+6:00) Asia/Dhaka
(GMT+6:00) Asia/Qyzylorda
(GMT+6:00) Asia/Thimbu
(GMT+6:00) Asia/Thimphu
(GMT+6:00) Asia/Yekaterinburg
(GMT+6:00) BST
(GMT+6:00) Etc/GMT-6
(GMT+6:00) Indian/Chagos

Solution

  • Use TimeZone#getAvailableIDs(int)

    import java.util.*;
    class Hello
    {
       public static void main (String[] args) throws java.lang.Exception
       {
         TimeZone tz = TimeZone.getDefault();
         int offset = 21600000;
         String[] availableIDs = tz.getAvailableIDs(offset);
         for(int i = 0; i < availableIDs.length; i++) {
           System.out.println(availableIDs[i]);
         }
       }
    }