cron expression for Spring scheduler - run only once a year

java1977 picture java1977 · Feb 11, 2016 · Viewed 11.1k times · Source

My Spring service loooks like

@Scheduled( cron="0 0  7 * * SUN")
public void doSomething() {
    // do something
}

I understand you can't have the 7th value which is reserved for specifying a year. using a expression can I tell spring to just run once a year at a particular time say 25th Dec 6 am in 2020 ?

Thanks

Answer

Erik Pragt picture Erik Pragt · Feb 11, 2016

Yes, you can. Just have a look at this answer. In short, you can use the format:

0 0 6 6 9 ? 
| | | | | | 
| | | | | |
| | | | | +----- any day of the week.
| | | | +------- 9th month (September).
| | | +--------- 6th day of the month.
| | +----------- 6th hour of the day.
| +------------- Top of the hour (minutes = 0).
+--------------- Top of the minute (seconds = 0).