Spring Cron Expression for Every Day 1:01:Am

Spring Cron expression every 15 mins for particular hours and not running on days

You can declare analytically what you want in such a complex scenario. The following should work.

@Scheduled(cron = "0 0/15 0,1,2,3,4,5,6,22,23 1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,26,27,28,29,30,31 * ? *")

Or you can summarize it a bit into

@Scheduled(cron = "0 0/15 0-6,22-23 1,2,4-24,26-31 * ? *")

For hour 24 is represented as 0

Spring cron scheduler for every 59 seconds do double run

I think there is a bug in the scheduler.

With the following test code You will see that 2 schedules occur when the current time is in the second 59 on in second 00.

  Set<String> x = new HashSet<>();
List<LocalDateTime> y = new ArrayList<>();
while (true) {

var expression = CronExpression.parse("*/59 * 9-21 * * *");
final LocalDateTime now = LocalDateTime.now();
y.add(now);
x.add(expression.next(now).toString());
System.out.println(x);
Thread.sleep(500);

if (x.size() > 2){
break;
}
}

System.out.println(x);

I obtained 3 results

[2022-02-09T15:58:59, 2022-02-09T15:59:59, 2022-02-09T15:59]

For the following dates:

Sample Image

Sample Image

I've tested this with Spring Boot version 2.6.3.

Spring cron expression for every after 30 minutes

<property name="cronExpression" value="0 0/30 * * * ?" />

“First Wednesday of the month at 9 AM” in cron?

You can use this syntax

0 9 1W * *

1W will specify the first Wednesday of the month.

The answer below does not appear to work (at least according to some online tools)


You can take advantage of the ability to specify multiple days in the 3rd parameter, like this:

0 9 1-7 * 3

Trying the expression in https://crontab.cronhub.io/, this is how the above gets described:

At 09:00 AM, between day 1 and 7 of the month, and on Wednesday

You can be sure that one and only one of the first 7 days of the month will be Wednesday, and is also the first Wednesday of the month.



Related Topics



Leave a reply



Submit