Search code examples
springcroncron-taskspring-scheduled

Cron expression isn't working as intended for spring boot


I wan't to have a method run every sunday at 1:30 PM so far I can get thing to run like every 10 minutes or at 1:30 every day, but now specific day

Here is what I have so far

 /**
     * Fires at 1:30 PM sundauy
     */
    @Scheduled(cron = "0 30 13 * 1 ?")
    fun sendNotifications() {

    }

I think the problem is in the day of month field possibly? or sunday isn't 1 indexed. I see in other con implementations its 0.


Solution

  • the 5th field is used for "month" while 6th field is for "day of week" (see here and here), so your expression ("0 30 13 * 1 ?") will fire your method at "13:30 of every day in January".

    weekday names can be given as the first three letters of the English names. in order to schedule a method to run "every sunday at 13:30" this expression can be used:

    @Scheduled(cron = "0 30 13 ? * SUN")