如何识别春季的 cron 条目是否适用于今天?



我的数据库中有一个 cron 条目列表,其中只有日期、月份、星期几的值。

我的数据库中的示例 cron 值。 "0 0 * ** " ">* * 15 2" ">* * * * 周一至周五"

如何检查我的数据库中是否有任何适用于今天的 cron 条目?我必须在我的 java Spring 引导应用程序中使用它。

You can use cronexpression of quartz to get for next valid fire date of cron.
Ex.
String orgTimeZone = "US/Eastern";
String cron = "0 10 2 ? * MON,WED *";
CronExpression exp = new CronExpression(cron);
exp.setTimeZone(TimeZone.getTimeZone(orgTimeZone));
Date date = new Date();
Date nextValidFireTime = exp.getNextValidTimeAfter(date);
ZonedDateTime nextExecDate = zonedDateTimeConverter.toZonedDateTime(nextValidFireTime, orgTimeZone);
ZonedDateTime currentDate = zonedDateTimeConverter.toZonedDateTime(date, orgTimeZone);
System.out.println(nextExecDate);
ZonedDateTime after1Hourdate = currentDate.plusHours(1);
System.out.println(currentDate + ":" + after1Hourdate);
if (nextExecDate.isAfter(currentDate) && nextExecDate.isBefore(after1Hourdate)) {
System.out.println("Match Cron");
}

最新更新