Java TimeZone getTimeZone() Method



Description

The Java TimeZone getTimeZone(String ID) method is used to get the TimeZone for the given ID.

Declaration

Following is the declaration for java.util.TimeZone.getTimeZone() method.

public static TimeZone getTimeZone(String ID)

Parameters

ID − This is the ID for a TimeZone.

Return Value

The method call returns the specified TimeZone, or the GMT zone if the given ID cannot be understood.

Exception

NA

Getting TimeZone of GMT-8 Example

The following example shows the usage of Java TimeZone getTimeZone() method to get the Time zone based on given Id. We've created a TimeZone using getDefault() method. Using getTimeZone() we're printing the TimeZone object of GMT - 8.

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create time zone object     
      TimeZone timezone = TimeZone.getDefault();

      // checking time zone value     
      System.out.println("Time zone: " + timezone.getTimeZone("GMT-8:00"));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Time zone: sun.util.calendar.ZoneInfo[id="GMT-08:00",offset=-28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

Getting TimeZone of IST Example

The following example shows the usage of Java TimeZone getTimeZone() method to get the Time zone based on given Id. We've created a TimeZone using getDefault() method. Using getTimeZone() we're printing the TimeZone object of IST.

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create time zone object     
      TimeZone timezone = TimeZone.getDefault();

      // checking time zone value     
      System.out.println("Time zone: " + timezone.getTimeZone("IST"));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Time zone:sun.util.calendar.ZoneInfo[id="IST",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]

Getting TimeZone of BST Example

The following example shows the usage of Java TimeZone getTimeZone() method to get the Time zone based on given Id. We've created a TimeZone using getDefault() method. Using getTimeZone() we're printing the TimeZone object of BST.

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create time zone object     
      TimeZone timezone = TimeZone.getDefault();

      // checking time zone value     
      System.out.println("Time zone: " + timezone.getTimeZone("BST"));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Time zone: sun.util.calendar.ZoneInfo[id="BST",offset=21600000,dstSavings=0,useDaylight=false,transitions=8,lastRule=null]
java_util_timezone.htm
Advertisements