I am facing an issue where I need the difference between 2 dates to be equal to 21 days.
The code that I have written is working as expected except 1 case
When the two dates fall under CET and CEST due to the daylight saving, the difference is never 21 days.
I tried setting the time zone to UTC, but it does not work.
Below is the code that I am using.
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
//forcing the parsing to UTC
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//forcing the parsing to UTC
String endDate = "03/30/2026";
Date leaverDate = null;
leaverDate = sdf.parse(endDate);
Date todayDateObj = sdf.parse("03/08/2026");
c.setTime(todayDateObj);
//get todays date with hh:mm:ss as 00:00:00
//Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Date todayDate = c.getTime();
// get identity start date with hh:mm:ss as 00:00:00
c.setTime(leaverDate);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
leaverDate = c.getTime();
// get difference in dates and return true if difference is equal to 7 days
long diff = leaverDate.getTime() - todayDate.getTime();
log.debug("difference in dates for 21 days email..." + diff);
//trigger workflow if 21 days
if(diff == 1814400000){
return "true";
}
else
return "false";
@rishavghoshacc Please test this outside IIQ first. You should use ChronoUnit.DAYS. It only counts a “day” if a full 24-hour cycle has completed. If 20 days and 23 hours have passed, it returns 20.
Also, as you are dealing with timezone, you should be using ZonedDateTime apis. which can help converting the date to the timezone before doing the comparision.
Note: Found a fix?Help the community by marking the comment as solution. Feel free to react(,, etc.)with an emoji to show your appreciation or message me directly if your problem requires a deeper dive.