I want to define multiple date blocks (school holidays) and compare now with those dates. Something like this:
rule "regelTestsDel2" when
Item del2 received command ON
then
val startDate = new DateTime(2025, 12, 23)
val endDate = new DateTime(2026, 01, 06)
if (now() >= startDate && now() <= endDate) {
logInfo("DatumCheck", "Holiday")
} else {
logInfo("DatumCheck", "Not holiday")
}
end
It was a long time since I used rules DSL, so not sure if there even is a DateTime class available? But LocalDate should work here:
rule "regelTestsDel2" when
Item del2 received command ON
then
val today = LocalDate.now()
val startDate = LocalDate.of(2025, 12, 23)
val endDate = LocalDate.of(2026, 01, 06)
if (today.isAfter(startDate) && today.isBefore(endDate)) {
logInfo("DatumCheck", "Holiday")
} else {
logInfo("DatumCheck", "Not holiday")
}
end
This would need to also check if today == startdate as well as today == enddate
In JRuby, all date/time operations are so simple and nice to write and to read.
logger.info Date.today.between?("2025-12-23", "2026-01-06") # include year
# Checking just month and date. Crossing year works as expected
logger.info Date.today.between?("12-23", "01-06")
This between? method is available, in JRuby, for just about any date/time related stuff, including Javaâs ZonedDateTime, LocalDate, LocalTime, MonthDay, Month, Duration, etc, and Rubyâs Time, Date, etc.
Furthermore you can use ranges too, and in ranges, you can use two dots (includes the end range), or three dots (excludes the end range), begin-less range, and end-less range
Lastly, if you want to use Ephemeris, you can enter your Holidays into a custom ephemeris file and after that, you simply do
holiday_file! OpenHAB::Core.config_folder / "holidays/my_holiday.xml"
if Time.now.holiday? # This can be #ZonedDateTime, or even a DateTimeItem
logger.info "Today is a holiday"
else
logger.info "Today is not a holiday"
end
#It works for a DateTime Item too!
if MyDateTimeItem.holiday?
logger.info "MyDateTimeItem contains #{MyDateTimeItem.state} which is a holiday"
end
Is that âtrueâ for 12/23/2025 and for 1/6/2026? That is, inclusive? âBetweenâ is ambiguous to me but maybe not to everyone else? Probably it is inclusive, just not sure.
Iâm not sure what you mean be inclusive in this context. But the test is whether time.toZDT() is after the start date and before the end date so 12/23/2025 would return false.
/**
* Tests whether `this` time.ZonedDateTime is between the passed in start and end.
* However, the function only compares the date portion of the three, ignoring the time portion.
*
* @param {*} start starting date, anything supported by {@link time.toZDT}
* @param {*} end ending date, anything supported by {@link time.toZDT}
* @returns {boolean} true if `this` is between start and end
*/
time.ZonedDateTime.prototype.isBetweenDates = function (start, end) {
const startDate = toZDT(start).toLocalDate();
const endDate = toZDT(end).toLocalDate();
const currDate = this.toLocalDate();
return currDate.isAfter(startDate) && currDate.isBefore(endDate);
};
And looking at the docs for isBefore and isAfter on the js joda library: LocalDate | js-joda
Checks if this date is before the specified date.
This checks to see if this date represents a point on the local time-line before the other date.