Java TimeZone class

java.util.TimeZone with concepts and examples of java.util.TimeZone class, java TimeZone methods, java TimeZone examples, get current timezone and more.

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 TimeZoneExample1 {  
  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.    }      


In TimeZone class available Ids are: 
Africa/Bissau and so on ....

Java TimeZone class Example: getOffset()

  1. import java.util.*;  
  2. public class TimeZoneExample2 {  
  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.    }      


The Offset value of TimeZone: 19800000

Java TimeZone class Example: getID()

  1. import java.util.*;  
  2. public class TimeZoneExample3 {  
  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.    }      


Value of ID is: Asia/Kolkata

Java TimeZone class Example: getDisplayName()

  1. import java.util.*;  
  2. public class TimeZoneExample4 {  
  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.    }      


Display name for default time zone: India Standard Time