Home » Java programming language

Java Calendar setTimeZone() Method with Example

Calendar Class setTimeZone() method: Here, we are going to learn about the setTimeZone() method of Calendar Class with its syntax and example.
Submitted by Preeti Jain, on February 02, 2020

Calendar Class setTimeZone() method

  • setTimeZone() method is available in java.util package.
  • setTimeZone() method is used to sets the current time from the given parameter (time_in_ms) of this Calendar.
  • setTimeZone() method is a non-static method, 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 set time in UTC milliseconds.

Syntax:

    public void setTimeZone(TimeZone tz);

Parameter(s):

  • TimeZone tz – represents the specified time zone.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java Program to demonstrate the example of
// void setTimeZone(TimeZone tz) method of 
// Calendar

import java.util.*;

public class SetTimeZone {
    public static void main(String args[]) {
        // Instantiating a Calendar object
        Calendar ca = Calendar.getInstance();
        TimeZone time_zo = TimeZone.getTimeZone("EST");

        // By using setTimeZone(TimeZone) method is to 
        // set the time zone of the current
        // Calendar

        ca.setTimeZone(time_zo);

        // Display Current Calendar TimeZone
        System.out.println("ca.getTimeZone().getDisplayName(): " + ca.getTimeZone().getDisplayName());
    }
}

Output

ca.getTimeZone().getDisplayName(): Eastern Standard Time



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.