Java TimeZone setDefault() Method



Description

The Java Timezone setDefault(TimeZone zone) method is used to set the TimeZone that is returned by the getDefault method.

Declaration

Following is the declaration for java.util.TimeZone.setDefault() method.

public static void setDefault(TimeZone zone)

Parameters

zone − This is the new default time zone

Return Value

NA

Exception

NA

Setting a Default Zone for Timezone of America Region Example

The following example shows the usage of Java TimeZone setDefault(TimeZone) method to set the TimeZone to be returned by the getDefault() method. We've created a TimeZone using America/Los_Angeles and then set it to default using setDefault() method. Then using getDefault() method, we've verified the result.

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create time zone object 
      TimeZone tzone = TimeZone.getTimeZone("America/Los_Angeles");

      // set time zone to default
      tzone.setDefault(tzone);

      // checking default time zone
      System.out.println("Default time zone:" +tzone.getDefault());
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Default time zone:sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

Setting a Default Zone for Timezone of Africa Region Example

The following example shows the usage of Java TimeZone setDefault(TimeZone) method to set the TimeZone to be returned by the getDefault() method. We've created a TimeZone using Africa/Abidjan and then set it to default using setDefault() method. Then using getDefault() method, we've verified the result.

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create time zone object 
      TimeZone tzone = TimeZone.getTimeZone("Africa/Abidjan");

      // set time zone to default
      tzone.setDefault(tzone);

      // checking default time zone
      System.out.println("Default time zone:" +tzone.getDefault());
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Default time zone:sun.util.calendar.ZoneInfo[id="Africa/Abidjan",offset=0,dstSavings=0,useDaylight=false,transitions=3,lastRule=null]

Setting a Default Zone for Timezone of Poland Region Example

The following example shows the usage of Java TimeZone setDefault(TimeZone) method to set the TimeZone to be returned by the getDefault() method. We've created a TimeZone using Poland and then set it to default using setDefault() method. Then using getDefault() method, we've verified the result.

package com.tutorialspoint;

import java.util.TimeZone;

public class TimeZoneDemo {
   public static void main( String args[] ) {

      // create time zone object 
      TimeZone tzone = TimeZone.getTimeZone("Poland");

      // set time zone to default
      tzone.setDefault(tzone);

      // checking default time zone
      System.out.println("Default time zone:" +tzone.getDefault());
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Default time zone:sun.util.calendar.ZoneInfo[id="Poland",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=165,lastRule=java.util.SimpleTimeZone[id=Poland,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]]
java_util_timezone.htm
Advertisements