Getting Information of All Available Time Zones

In this section, the given program shows you how to get about all the information for the time zones.

Getting Information of All Available Time Zones

Getting Information of All Available Time Zones

     

Time Zone: Time Zone is the specified part of the earth that holds the special standard of the time i.e. termed as local time. Usually time zone is based on the GMT (Greenwich Mean Time). Each and every part of the earth has categorized for holding it's own time zones that had been fixed according to the rotation of the earth on it's own base.

Most application software uses the underlying operating system for getting all information about the time zones for setting up the time according to the appropriate time zones. Java maintains it's own time zones database as well as the operating system database. There need to update the java time zone database from the operating system database when it is changed.

DST (Daylight Saving Time): Daylight Saving Time is also called the summer time somewhere in many countries. In this period the sun rises in the morning and sets in the evening one hour later. So, the evening became one hour longer. Benefit of the period is saving energy because in this period artificial light is less needed during the evening. To make DST work, when DST begins, clocks have to be adjusted one hour ahead and adjusted one hour back to standard time every autumn. Many countries maintain the DST and many do not.

Up to now, most of the United States have observed Daylight Saving Time from the first Sunday of April at 2:00 am to the last Sunday of October at 2:00 am. In 2007, most of the U.S. will begin Daylight Saving Time from the second Sunday of March at at 2:00 a.m. and revert to standard time on the first Sunday in November. In the U.S., each time zone switches at a different time.

Program Result:

In this section, the given program shows you how to get about all the information for the time zones. This program shows all time zones available in the world with the Time Zone ID, Time Zone Name and it's fixed GMT (India Standard Time : 5:30).

Code Description:

TimeZone:
This class is used to getting and setting the time zone. You can get the default time zone using the getDefault() method i.e. the time zone on where your program is running.

TimeZone.getAvailableIDs():
Above method returns a array of all time zone ids like the time zone id for the time zone of India is Asia/Calcutta.

TimeZone.getTimeZone(id):
Above method returns the full name of the Time Zone. The time zone id, which full name has to be found, is passed through the method as a parameter.

TimeZone.getDisplayName(TimeZone.inDaylightTime(new Date()), TimeZone.LONG):
Above code of the program represents two methods and a field of the TimeZone class these are explained as follows:

  • TimeZone.LONG:
    This field of the TimeZone class returns a static integer value which is used in the getDisplayName() method of the class as a parameter for specifying the style of the name of the time zone. It determines the long (full) name of the specific time zone. Another field is the TimeZone.SHORT which determines the short name of the time zone.
      
  • TimeZone.inDaylightTime(new Date()):
    This method returns the abstract boolean type value after checking whether the given date is in the Daylight Saving Time period or not. It returns true, if the date is in the DST (Daylight Saving Time) otherwise false.
      
  • TimeZone.getDisplayName(TimeZone.inDaylightTime(new Date()), TimeZone.LONG):
    This method gives you the full name of the specified time zone. This method returns String type value which is the name of the time zone for the specified Locale.

TimeZone.getRawOffset():
Above method returns the amount of time to add UTC () to get standard time for the specified time zone. This method returns the static integer value which is the amount of time in millisecond.

Math.abs(real_number):
This method returns the absolute number for the given number. The number, which has to become absolute, is passed through the method as a parameter.

Here is the code of the program:

import java.util.*;

public class TimeZones{
  public static void main(String[] args){
  Date date = new Date();
  String TimeZoneIds[] = TimeZone.getAvailableIDs();
  for(int i = 0; i < TimeZoneIds.length; i++){
  TimeZone tz = TimeZone.getTimeZone(TimeZoneIds[i]);
  String tzName = 
  tz.getDisplayName
(tz.inDaylightTime(date), TimeZone.LONG);
  System.out.print(TimeZoneIds[i": ");
  // Get the number of hours from GMT
  int rawOffset = tz.getRawOffset();
  int hour = rawOffset / (60*60*1000);
  int minute = Math.abs(rawOffset / (60*1000)) 60;
  System.out.println(tzName + " " + hour + ":" + minute);
  }
  }
}

Download this example.