Home > Software design >  how to handle date comparison in Java during daylight saving?
how to handle date comparison in Java during daylight saving?

Time:12-09

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?

CodePudding user response:

I am not sure what's the actual library you are using here, so I can't provide a code example, but basically you could use LocalDate or something similar and configure it in your timezone. This would know about the daylight savings.

CodePudding user response:

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())
  • Related