Home »
Java programming language
Java GregorianCalendar setTimeZone() Method with Example
GregorianCalendar Class setTimeZone() method: Here, we are going to learn about the setTimeZone() method of GregorianCalendar Class with its syntax and example.
Submitted by Preeti Jain, on February 15, 2020
GregorianCalendar Class setTimeZone() method
- setTimeZone() method is available in java.util package.
- setTimeZone() method is used to sets the time zone with the specified TimeZone(tz) for this GregorianCalendar.
- setTimeZone() method is a non-static method, so it is accessible with the class object and if we try to access the method with the class name then we will get an error.
- setTimeZone() method does not throw an exception at the time of the set time zone.
Syntax:
public void setTimeZone(TimeZone tz);
Parameter(s):
- TimeZone tz – represent the time zone to be set for this calendar.
Return value:
The return type of the method is void, it returns nothing.
Example:
// Java program is to demonstrate the example of
// setTimeZone(TimeZone tz) method of GregorianCalendar
import java.util.*;
public class SetTimeZoneOfGregorianCalendar {
public static void main(String[] args) {
// Instantiates a GregorianCalendar object
GregorianCalendar ca = (GregorianCalendar) GregorianCalendar.getInstance();
TimeZone tmz = TimeZone.getTimeZone("GMT");
// Display current GregorianCalendar
System.out.println("ca: " + ca.getTime());
// By using setTimeZone() method isto
// set the time zone of this GregorianCalendar
ca.setTimeZone(tmz);
// By using getTimeZone() method isto
// get the time zone of this GregorianCalendar
System.out.print("ca.setTimeZone(tmz): ");
System.out.println(ca.getTimeZone().getDisplayName());
}
}
Output
ca: Sat Feb 15 13:00:55 GMT 2020
ca.setTimeZone(tmz): Greenwich Mean Time