devxlogo

How to Check if DayLight Saving Is Present for a TimeZone

How to Check if DayLight Saving Is Present for a TimeZone

See how to figure out whether or not a given TimeZone supports the Daylight Saving feature and if it is in use. Java supports this with the help of observesDaylightTime() method.

import java.util.TimeZone;   public class DayLightDetails {     public static void main(String[] args)     {         DayLightDetails dayLightDetails = new DayLightDetails();      dayLightDetails.proceed();   }      public void proceed()   {      //DayLight savings supported TimeZone - US/Mountain      //Timezone object being initialized with US/Mountain        System.out.println("TimeZone: US/Mountain");      TimeZone timeZone = TimeZone.getTimeZone("US/Mountain");         System.out.println("Initialized timeZone: " + timeZone);               //Checking if the day light is being observed        System.out.println("timeZone.observesDaylightTime(): " + timeZone.observesDaylightTime());             //DayLight savings un-supported TimeZone - Asia/Bangkok      System.out.println("");      System.out.println("TimeZone: Asia/Bangkok");      timeZone = TimeZone.getTimeZone("Asia/Bangkok");         System.out.println("Initialized timeZone: " + timeZone);               //Checking if the day light is being observed        System.out.println("timeZone.observesDaylightTime(): " + timeZone.observesDaylightTime());           } } /*

Expected output:

[root@mypc]# java DayLightDetailsTimeZone: US/MountainInitialized timeZone: sun.util.calendar.ZoneInfo[id="US/Mountain",offset=-25200000,dstSavings=3600000,useDaylight=true,transitions=157,lastRule=java.util.SimpleTimeZone[id=US/Mountain,offset=-25200000,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]]timeZone.observesDaylightTime(): trueTimeZone: Asia/BangkokInitialized timeZone: sun.util.calendar.ZoneInfo[id="Asia/Bangkok",offset=25200000,dstSavings=0,useDaylight=false,transitions=3,lastRule=null]timeZone.observesDaylightTime(): false*/

devx-admin

Share the Post: