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:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println( "" + cal.getTime());
System.out.println( "Current Time Zone : " +
cal.getTimeZone().getDisplayName());
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:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println( "" + cal.getTime());
System.out.println( "Current Time Zone : " +
cal.getTimeZone().getDisplayName());
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()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Jul, 2018
Like Article
Save Article