The java timing task cron expression is executed once a week, and the execution time is different from the actual time!!!

It is a very common function for java springboot to use schedule to execute timing tasks. There is a very commonly used website is the online Cron expression generator, but recently encountered a pit on this website.

I want to execute it once every Monday at 1:00 pm. I verified the expression I wrote on this website, and it is ok, and there is no problem comparing it with the calendar, but now the pitfall has appeared!!!

 

 But looking at the server log, I found that the execution was performed on May 16 (Tuesday), which is one day later than the scheduled task setting time

This, this, this is too outrageous!!!!

After verification, if it is executed once a week, Monday is 1 and Tuesday is 2, remember not to add 1 to the date!!!

For example: execute once every Monday at 1:00 pm:

@Scheduled(cron = "0 0 13 ? * 1")

The reason:

Different representations of cron expressions in Quartz and springboot schedules about the week (day of the week)

Quartz official source code (org.quartz.CronExpression) explanation:

The cron expression in Quartz can be composed of up to 7 fields, namely: second, minute, hour, day, month, week, year, and the last field "year" can be empty;

For the day of the week, that is, "Day-of-Week", the values ​​1, 2, 3, 4, 5, 6, and 7 respectively represent "SUN, MON, TUE, WED, THU, FRI, SAT";

SpringBoot schedule cron expression analysis:

Replace "SUN, MON, TUE, WED, THU, FRI, SAT" in the cronExpression string with "0, 1, 2, 3, 4, 5, 6" respectively;

Summary: If you can't grasp it, don't use numbers to express the day of the week, and use English abbreviations directly: UN, MON, TUE, WED, THU, FRI, SAT

Guess you like

Origin blog.csdn.net/zlfjavahome/article/details/132566784