dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 23, 2019

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 DayLightDetails
TimeZone: US/Mountain
Initialized 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(): true

TimeZone: Asia/Bangkok
Initialized timeZone: sun.util.calendar.ZoneInfo[id="Asia/Bangkok",offset=25200000,dstSavings=0,useDaylight=false,transitions=3,lastRule=null]
timeZone.observesDaylightTime(): false

*/
M S Sridhar
 
Thanks for your registration, follow us on our social networks to keep up-to-date