javacroncron-taskcronexpressionspring-scheduled

Cron expression to run the task at 11am finished at 11pm


I need cron expression, to schedule my task. The task execution should start every day at 11am and should be executed every minute till 11pm( the last execution time).

Currently, I don't know how to set that last execution should be at 23:00.

* * 11-23 * * * - According to this expression the task would be run from 11:00 till 23:59.

* * 11-22 * * *- According to this expression the task would be run from 11:00 till 22:59. So the last execution for 23:00 is missed.

Please, tell me how can I solve this.


Solution

  • I have find work around for the issue. The solution is to create two cron expression:

    1. 0 * 11-22 * * * - this will start at 11am and finish at 22:59pm.

    2. 0 0 23 * * * - this task will start only once every day at 23:00.

    So, my code now look like this:

    @Scheduled(cron = "0 * 11-22 * * *")
      public void processPerformances() {
        // do something();
    
      }
    
      @Scheduled(cron = "0 0 23 * * *")
      public void processPerformancesLastTime() {
        processPerformances();
      }