Tag Archives: cron expression

Spring scheduler – cron expression

When working with spring scheduler, you will see we can use cron expression to configure when scheduled task will run. In this post  i will help you to know how cron expression work.

Cron expression is six fields that separate by single space. Below is ordinary of field from left to right with possible values.

Position Name Possible Values Special Character
1 Second 0-59 , / * -
3 Hour 0-23 , / * -
4 Day of month 1-31 , / * – ?
5 Month 1-12 or JAN-DEC , / * -
6 Week day 1-7 or MON-SUN , / * – ?

Note:

  • ,: list possible values
  • / sequence
  • *: every possible values
  • -: rage from two numbers
  • ?: unspecific value

Some examples:

  • 0/5 * * * * *: for every five second
  • * 0-59/2 * * * *: for every two minutes: 0,2,4,8…58
  • * * 2,14 * * MON-FRI: run on 2am and 2 pm from Monday to Friday