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:
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
Rounding Up a Number to Nearest Multiple of 5
How to Pass an Integer Class Correctly by Reference
Why Doesn't Java.Lang.Number Implement Comparable
How to Get the Unique Id of an Object Which Overrides Hashcode()
Soap Request to Webservice with Java
What Does an Integer That Has Zero in Front of It Mean and How to Print It
Is the in Relation in Cassandra Bad for Queries
Differencebetween Optional.Flatmap and Optional.Map
JPA & Criteria API - Select Only Specific Columns
How to Get a Token from a Lucene Tokenstream
Java Command Line with External .Jar
How to Set Connection Timeout with Okhttp
Java Server with Multiclient Communication
How to Get a List from Some Class Properties with Java 8 Stream