how to use TimeZone class using collections in java
Previous | Home | Next |
In this example we shall see how to use the TimeZone class in collections.
For using the TimeZone class we have to import the java.util package which contain the java.util.TimeZone class. Usually you get a TimeZone using getDefault() method which creates a TimeZone based on the time zone where the program is running.
In the following example we have used the following methods,
static TimeZone getDefault()
This method gets the default TimeZone for the current host.
static TimeZone getTimeZone(String stringID)
This method gets you the TimeZone for the specified ID.
String getDisplayName()
This method returns a name of the current time zone suitable for presentation to the user in the default locale.
static String[] getAvailableIDs()
The above method gets all the available IDs supported by the TimeZone class.
String getID()
This method gets the ID of the current TimeZone.
boolean useDaylightTime()
This method checks whether the TimeZone uses daylight savings time or not.
We have created a class named CollectionsExample, and then we have created a TimeZone reference object named timezonedefault and initialized it with the static method getDefault() on the TimeZone class. Then we have displayed the ID of the default time zone.
We have created another TimeZone object timezone initialized it by using the getTimeZone(String stringID) method on the TimeZone class. Then we have get its ID and checked whether this TimeZone object used the DaylightTime or not.
Then we have created a String[] timezoneids for storing the array of the values returned by invoking the getAvailableIDs() method on the TimeZone class. Then, we have printed all the values of the IDs of the TimeZone class.
package r4r.co.in;
import java.util.*;
public class CollectionsExample {
/**
* @param args
*/
public static void main(String[] args) throws IllegalArgumentException
{
// TODO Auto-generated method stub
try
{
TimeZone timezonedefault=TimeZone.getDefault(); // this creates a TimeZone object with default value of TimeZone
System.out.println("The id of defautl TimeZone object is :"+timezonedefault.getID()); // we are displaying the ID of default TimeZone object
TimeZone timezone = TimeZone.getTimeZone("Asia/Calcutta"); // here we are creating a TimeZone object of Asia/Calcutta .i.e. India
System.out.println("The display name of the timezone is: "+timezone.getDisplayName()); // here we are displaying the name of timezone
System.out.println("The RawOffset is given as: "+timezone.getRawOffset()); // here we are displaying the RawOffset of the timezone
System.out.println("The timezone uses daylight or not: "+timezone.useDaylightTime()); // checks whether the timezone uses the daylight savings time
int hour=(timezone.getRawOffset())/(60*60*1000); // gets the number of hours from GMT
System.out.println("The number of hours from GMT:"+hour); // displays the number of hours
System.out.println(timezone.getID()); // here we are getting the ID of timezone
timezone.setID("This is my id"); // now we are changing the ID of the timezone
System.out.println(timezone.getID()); // again we are displaying the ID of the timezone
String[] timezoneids = TimeZone.getAvailableIDs(); // here we are storing all the available ID's of TimeZone in an String array
System.out.println("The list of TimeZone ID's is as follows: ");
for(int i=0; i<timezoneids.length; i++)
{
System.out.print(timezoneids[i]+" , "); // displaying all the ids
}
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
}
}
The output of the above example is given as:
The id of defautl TimeZone object is :America/Los_Angeles
The display name of the timezone is: India Standard Time
The RawOffset is given as: 19800000
The timezone uses daylight or not: false
The number of hours from GMT:5
Asia/Calcutta
This is my id
The list of TimeZone ID's is as follows:
Etc/GMT+12 , Etc/GMT+11 , MIT , Pacific/Apia , Pacific/Midway , Pacific/Niue , Pacific/Pago_Pago , Pacific/Samoa , US/Samoa , America/Adak , America/Atka , Etc/GMT+10 , HST , Pacific/Fakaofo , Pacific/Honolulu , Pacific/Johnston , Pacific/Rarotonga , Pacific/Tahiti , SystemV/HST10 , US/Aleutian , US/Hawaii , Pacific/Marquesas , AST , America/Anchorage , America/Juneau , America/Nome , America/Yakutat , Etc/GMT+9 , Pacific/Gambier , SystemV/YST9 , SystemV/YST9YDT , US/Alaska , America/Dawson , America/Ensenada , America/Los_Angeles , America/Tijuana , America/Vancouver , America/Whitehorse , Canada/Pacific , Canada/Yukon , Etc/GMT+8 , Mexico/BajaNorte , PST , PST8PDT , Pacific/Pitcairn , SystemV/PST8 , SystemV/PST8PDT , US/Pacific , US/Pacific-New , America/Boise
Previous | Home | Next |