The java.util.GregorianCalendar.getTimeZone() method is an in-built method in Java which fetches the time zone and returns the TimeZone object associated with this calendar.
Syntax:
public TimeZone getTimeZone()
Parameters: This method does not accept any parameter.
Return Values: This method returns a TimeZone object which denotes the time zone of this calendar.
Examples:
Input : Mon Jul 23 19:45:33 UTC 2018
Output : Coordinated Universal Time
Input : Wed Oct 23 20:02:52 UTC 2019
cal.setTimeZone(TimeZone.getTimeZone("CST"));
Output : Central Standard Time
Below programs illustrate the java.util.GregorianCalendar.getTimeZone() function in Java:
Program 1:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println( "Current Date and Time : "
+ cal.getTime());
TimeZone t = cal.getTimeZone();
System.out.println( "Time Zone : " +
t.getDisplayName());
}
}
|
Output:
Current Date and Time : Wed Jul 25 11:25:16 UTC 2018
Time Zone : Coordinated Universal Time
Program 2: In this program, we use setTimeZone() to change the current time zone and then fetch the new time zone using getTimeZone() method.
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) {
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println( "Current Date and Time : "
+ cal.getTime());
cal.setTimeZone(TimeZone.getTimeZone( "CST" ));
TimeZone t = cal.getTimeZone();
System.out.println( "Time Zone : " +
t.getDisplayName());
}
}
|
Output:
Current Date and Time : Wed Jul 25 11:25:22 UTC 2018
Time Zone : Central Standard Time
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#getTimeZone()
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
27 Jul, 2021
Like Article
Save Article