Wednesday, July 3, 2024
HomeLanguagesJavaCalendar setTimeZone() Method in Java with Examples

Calendar setTimeZone() Method in Java with Examples

The setTimeZone(TimeZone time_zone) method in Calendar class takes a Time Zone value as a parameter and modifies or set the timezone represented by this Calendar.

Syntax:

public void setTimeZone(TimeZone time_zone)

Parameters: The method takes one parameter time_zone of Date type and refers to the given date that is to be set.

Return Value: The method does not return any value.

Below programs illustrate the working of setTimeZone() Method of Calendar class:
Example 1:




// Java code to illustrate
// setTime() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String[] args)
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the current time zone
        String tz_name = calndr.getTimeZone()
                             .getDisplayName();
  
        System.out.println("The Current Time"
                           + " Zone: " + tz_name);
  
        TimeZone time_zone
            = TimeZone.getTimeZone("GMT");
  
        // Modifying the time zone
        calndr.setTimeZone(time_zone);
  
        // Displaying the modified zone
        System.out.println("Modified Zone: "
                           + calndr.getTimeZone()
                                 .getDisplayName());
    }
}


Output:

The Current Time Zone: Coordinated Universal Time
Modified Zone: Greenwich Mean Time

Example 2:




// Java code to illustrate
// setTimeZone() method
  
import java.util.*;
  
public class Calendar_Demo {
    public static void main(String[] args)
    {
  
        // Creating calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the current time zone
        String tz_name = calndr.getTimeZone()
                             .getDisplayName();
  
        System.out.println("The Current Time"
                           + " Zone: " + tz_name);
  
        TimeZone time_zone
            = TimeZone.getTimeZone("Pacific/Tahiti");
  
        // Modifying the time zone
        calndr.setTimeZone(time_zone);
  
        // Displaying the modified zone
        System.out.println("Modified Zone: "
                           + calndr.getTimeZone()
                                 .getDisplayName());
    }
}


Output:

The Current Time Zone: Coordinated Universal Time
Modified Zone: Tahiti Time

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments