Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to use TimeZone class using collections in java

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