Skip to content
Related Articles

Related Articles

TimeZone setDefault() Method in Java with Examples
  • Last Updated : 29 Apr, 2019
GeeksforGeeks - Summer Carnival Banner

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:






// 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);
    }
}
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:




// 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);
    }
}
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)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :