javaspringspring-bootcronspring-scheduled

Setting Spring Scheduler to run every month when there are six days left until the end of the month


Is it possible to set Spring Scheduler to run every month when there are six days left until the end of the month ?

If so, what would be the CRON expression for this to happen ?


Solution

  • Since Spring 5.3, CronExpression supports L as the last day of the month. So you can use this kind of expression for your case:

    0 0 0 L-6 * *
    

    This will run at midnight 6 days before the end of month, accordingly to https://crontab.cronhub.io/

    Check also this pages:

    https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/support/CronExpression.html