Java .getTimeZone()

board9545756951's avatar
Published Sep 12, 2023
Contribute to Docs

The .getTimeZone() method in the Calendar class is used to return the current time-zone of a given calendar.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

time_zone = myCalendar.getTimeZone();
  • myCalendar is a Calendar class object for which the time zone is being retrieved.
  • time_zone is a TimeZone class object returned from the .getTimeZone() method.

Example

The following code demonstrates the use of the .getTimeZone() method.

import java.util.*;
public class Calendar_Demo {
public static void main(String args[])
{
// Creating a calendar object
Calendar calndr = Calendar.getInstance();
// Getting the time zone of calendar
TimeZone time_zone = calndr.getTimeZone();
// Displaying the current time zone
System.out.println("The current Time zone is: "
+ time_zone.getDisplayName());
}
}

The above code will return an output similar to:

The current Time zone is: Coordinated Universal Time

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours