Skip to content
Advertisement

how to handle date comparison in Java during daylight saving?

Not to run code at 5:30 Morning in Summer and not to run code on 6:30 in winter.

In Java Code, Applied following

scheduler (30 mins)
if(time.equals(5:30))
///exit code

but now winter comes and we need not not run it at 6:30 AM

How to handle this use case for daylight saving areas?

Advertisement

Answer

You can use TimeZone‘s inDaylightTime() method to determine if day light saving is applied or not e.g.

if(TimeZone.getTimeZone("US/Alaska").inDaylightTime(new Date())){

   // when daylight is true

}
else{
   // Code when daylight saving is false

}


For machine’s default time zone, you can check like

TimeZone.getDefault().inDaylightTime(new Date())
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement