Difference between * and ? in Spring @Scheduled(cron=".....")

Jordan picture Jordan · May 20, 2015 · Viewed 11.7k times · Source

I've been looking at the Spring Boot example for scheduling tasks (https://spring.io/guides/gs/scheduling-tasks/) and reading through some documentation (https://javahunter.wordpress.com/2011/05/05/cronscheduler-in-spring/) and I see that * and ? are used almost interchangeably.

For example, the line

@Scheduled(cron = "0 15 10 ? * *")

and

@Scheduled(cron = "0 15 10 * * ?")

do the exact same thing. So what is the difference between * and ?

Answer

Luchostein picture Luchostein · Jun 11, 2015

The tutorial is outdated. The symbol ? means exactly the same than *. As of Spring 3.1.2.RELEASE, the call hierarchy is:

The constructor CronTrigger(String) calls the constructor CronSequenceGenerator(String) which calls parse(String) which calls setDays(BitSet bits, String field, int max). Its implementation is clear:

private void setDays(BitSet bits, String field, int max) {
    if (field.contains("?")) {
        field = "*";
    }
    setNumberHits(bits, field, 0, max);
}

So, if ?, then *.