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*/
See also  Java Developer Jobs: Your Guide to Landing the Perfect Position in 2024
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist