Thursday, May 14, 2026
HomeLanguagesJavaGregorianCalendar setTimeZone() Method in Java

GregorianCalendar setTimeZone() Method in Java

The java.util.GregorianCalendar.setTimeZone() method is an in-built method in Java which modifies the current time zone to a new time zone according to the parameter passed.

Syntax:

public void setTimeZone(TimeZone tz)

Parameters: The method takes one parameter tz of TimeZone object which specifies the new time zone value.

Return Values: This method does not return any value.

Examples:

Input : IST
Output : India Standard Time

Input : UTC
Output : Coordinated Universal Time

Below programs illustrate the java.util.GregorianCalendar.setTimeZone() function in Java:

Program 1:




// Java Program to  illustrate 
// GregorianCalendar.setTimeZone()
// function 
  
import java.io.*;
import java.util.*;
  
class GFG {
     public static void main(String[] args) {
      
      // Create a new calendar
      GregorianCalendar cal = (GregorianCalendar) 
                    GregorianCalendar.getInstance();
  
      // Display the current date and time
      System.out.println("" + cal.getTime());
        
      System.out.println("Current Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
        
      // Convert to another time zone
      cal.setTimeZone(TimeZone.getTimeZone("CST"));
      System.out.println("New Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
   }
}


Output:

Wed Jul 25 11:11:14 UTC 2018
Current Time Zone : Coordinated Universal Time
New Time Zone : Central Standard Time

Program 2:




// Java Program to  illustrate 
// GregorianCalendar.setTimeZone()
// function 
  
import java.io.*;
import java.util.*;
  
class GFG {
     public static void main(String[] args) {
      
      // Create a new calendar
      GregorianCalendar cal = (GregorianCalendar) 
                    GregorianCalendar.getInstance();
  
      // Display the current date and time
      System.out.println("" + cal.getTime());
        
      System.out.println("Current Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
        
      // Convert to another time zone
      cal.setTimeZone(TimeZone.getTimeZone("IST"));
      System.out.println("New Time Zone : " + 
                    cal.getTimeZone().getDisplayName());
   }
}


Output:

Wed Jul 25 11:11:21 UTC 2018
Current Time Zone : Coordinated Universal Time
New Time Zone : India Standard Time

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#setTimeZone()

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS