TimeZone setDefault() Method in Java with Examples

The setDefault(TimeZone zone) method of TimeZone class in Java is used to set the TimeZone of the object that is returned by the getDefault() method of the TimeZone class.

Syntax:

public static void 
    setDefault(TimeZone zone)

Parameters: The method takes one parameter zone of TimeZone type which refers to the new default timezone.

Return Value: The method does not return any value.

Below program illustrates the working of setDefault() Method of TimeZone.
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate setDefault() method
  
import java.util.*;
  
public class TimeZoneDemo {
    public static void main(String args[])
    {
        // Creating an object of TimeZone class.
        TimeZone time_zone_default
            = TimeZone.getTimeZone("Europe/Rome");
  
        time_zone_default.setDefault(time_zone_default);
  
        // Displaying the default TimeZone
        System.out.println("Default TimeZone: "
                           + time_zone_default);
    }
}

chevron_right


Output:

Default TimeZone: sun.util.calendar.ZoneInfo[id="Europe/Rome", offset=3600000,
dstSavings=3600000, useDaylight=true, transitions=169, lastRule=java.util.SimpleTimeZone
[id=Europe/Rome, offset=3600000, dstSavings=3600000, useDaylight=true, startYear=0,
startMode=2, startMonth=2, startDay=-1, startDayOfWeek=1, startTime=3600000, startTimeMode=2,
endMode=2, endMonth=9, endDay=-1, endDayOfWeek=1, endTime=3600000, endTimeMode=2]]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate setDefault() method
  
import java.util.*;
  
public class TimeZoneDemo {
    public static void main(String args[])
    {
        // Creating an object of TimeZone class.
        TimeZone time_zone_default
            = TimeZone.getTimeZone("Pacific/Pago_Pago");
  
        time_zone_default.setDefault(time_zone_default);
  
        // Displaying the default TimeZone
        System.out.println("Default TimeZone: "
                           + time_zone_default);
    }
}

chevron_right


Output:

Default TimeZone: sun.util.calendar.ZoneInfo[id="Pacific/Pago_Pago",
offset=-39600000, dstSavings=0, useDaylight=false, transitions=3, lastRule=null]

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.