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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.