Java TimeZone class

Java TimeZone class

Java TimeZone class represents a time zone offset, and also figures out daylight savings. It inherits the Object class.


Java TimeZone class declaration

Let’s see the declaration of java.util.TimeZone class.

  1. public abstract class TimeZone extends Object  implements Serializable, Cloneable

Methods of Java TimeZone

Method Description
static String[] getAvailableIDs() It is used to get all the available IDs supported.
static TimeZone getDefault() It is used to get the default TimeZone for this host.
String getDisplayName() It is used to return a name of this time zone suitable for presentation to the user in the default locale.
String getID() It is used to get the ID of this time zone
int getOffset(long date) It is used to return the offset of this time zone from UTC at the specified date.
void setID(String ID) It is used to set the time zone ID

 

Java TimeZone class Example: getAvailableIDs()

  1. import java.util.*;
  2. public class TimeZoneExample {
  3.    public static void main( String args[] ){
  4.    String[] id = TimeZone.getAvailableIDs();
  5.    System.out.println(“In TimeZone class available Ids are: “);
  6.    for (int i=0; i<id.length; i++){
  7.    System.out.println(id[i]);
  8.    }
  9.    }
  10. }

Output:

In TimeZone class available Ids are: 
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau and so on ....

Java TimeZone class Example: getOffset()

  1. import java.util.*;
  2. public class TimeZoneExample {
  3.    public static void main( String args[] ){
  4.    TimeZone zone = TimeZone.getTimeZone(“Asia/Kolkata”);
  5.    System.out.println(“The Offset value of TimeZone: “ +
  6.    zone.getOffset(Calendar.ZONE_OFFSET));
  7.    }
  8. }

Output:

The Offset value of TimeZone: 19800000

Java TimeZone class Example: getID()

  1. import java.util.*;
  2. public class TimeZoneExample {
  3.    public static void main( String args[] ){
  4.    TimeZone timezone = TimeZone.getTimeZone(“Asia/Kolkata”);
  5.    System.out.println(“Value of ID is: “ + timezone.getID());
  6.    }
  7. }

Output:

Value of ID is: Asia/Kolkata

Java TimeZone class Example: getDisplayName()

  1. import java.util.*;
  2. public class TimeZoneExample {
  3.    public static void main( String args[] ){
  4.    TimeZone zone = TimeZone.getDefault();
  5.    String name = zone.getDisplayName();
  6.    System.out.println(“Display name for default time zone: “+ name);
  7.    }
  8. }

Output:

Display name for default time zone: India Standard Time
Next TopicJava DateFormat
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java TimeZone class Java TimeZone class represents a time zone offset, and also figures out daylight savings. It inherits the Object class. Java TimeZone class declaration Let’s see the declaration of java.util.TimeZone class. public abstract class TimeZone extends Object  implements Serializable, Cloneable Methods of Java TimeZone Method Description static String[] getAvailableIDs() It is used to get all the available IDs supported. static TimeZone […]

Tags:

....
...