Home » Java programming language

Java TimeZone setDefault() Method with Example

TimeZone Class setDefault() method: Here, we are going to learn about the setDefault() method of TimeZone Class with its syntax and example.
Submitted by Preeti Jain, on March 13, 2020

TimeZone Class setDefault() method

  • setDefault() method is available in java.util package.
  • setDefault() method is used to assign the default time zone which is retrieved by using the getDefault().
  • setDefault() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • setDefault() method does not throw an exception at the time of setting the default time zone.

Syntax:

    public static void setDefault(TimeZone tz);

Parameter(s):

  • TimeZone tz – represents the set default time zone.

Return value:

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

Example:

// Java program to demonstrate the example 
// of void setDefault(TimeZone tz)
// method of TimeZone 

import java.util.*;

public class SetDefaultOfTimeZone {
    public static void main(String args[]) {
        // Instantiates TimeZone object
        TimeZone tz = TimeZone.getDefault();

        // Display tz
        System.out.println("TimeZone.getDefault(): " + tz);

        // Get another time zone and set as default 
        // time zone
        tz = TimeZone.getTimeZone("Africa/Asmera");

        // By using setDefault() method is
        // to set the given time zone as default
        // time zone
        TimeZone.setDefault(tz);

        System.out.print("TimeZone.setDefault(tz): ");
        System.out.println(tz);
    }
}

Output

TimeZone.getDefault(): sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
TimeZone.setDefault(tz): sun.util.calendar.ZoneInfo[id="Africa/Asmera",offset=10800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]


Comments and Discussions!

Load comments ↻





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