Jay Trivedi
Jay Trivedi

Reputation: 170

Cron expression for daily start at 09.30 AM with 30 minute interval

I want my schedule job to run daily between specific time and at spefic interval.

For example interval of 30 minute starting from 09.30AM to 11.30PM.

Start time, end time and interval will be configurable at run time.

I tried with following cron expression : 0 30/30 09-23 1/1 * ?

But this runs at every hour instead of every 30 minutes.

If not possible with cronexpression then appreciate any way of doing it with java.

Note: start time, end time and interval must be configurable at run time

Upvotes: 3

Views: 16420

Answers (2)

xdzc
xdzc

Reputation: 1471

I know this is quite late but might help someone else eventually. You don't really need multiple expressions to achieve your goal.

0,30 9-13 * * MON-FRI // Every 30 mins from 9:00AM to 1:30PM weekdays
30 9-13 * * MON-FRI // Every 30 mins from 9:30AM to 1:30PM weekdays

Upvotes: 7

Thomas
Thomas

Reputation: 88707

As already stated in the comments you might need more than one expression. Assuming you restrict the interval to factors of 60 (i.e. 1,2,3,4,5,6,10,15,20,30,60) you should need 1 to 3 expressions.

Example: if you'd say every 5 minutes starting from 9:45 to 23:15 you'd need the following expressions:

0   45/5      9 * * ?   //every 5 minutes from 9:45 to 9:59
0    0/5  10-22 * * ?   //every 5 minutes from 10:00 to 22:59
0 0-15/5     23 * * ?   //every 5 minutes from 23:00 to 23:15

You should be able to calculate that from just the data you got. Here's a quick hack to get you started:

public static List<String> getExpressions( int startHour, int startMinute, int endHour, int endMinute, int interval) {
  List<String> expressions = new ArrayList<>();

  //If the start minute is greater than the interval we need a separate expression for the first hour
  if( startMinute >= interval ) {     
    expressions.add( String.format( "0 %d/%d %d * * ?", startMinute, interval, startHour ) );

    //the main expression needs to start as early as possible in the second hour, 
    //e.g. if you start at 9:33 and have 5 minute intervals it would need to start at 10:03  (9+1 = 10, 33%5 = 3)
    startMinute %= interval;
    startHour++;
  }

  //If the end minute is lower than the last run in the end hour we need a separate epxression for the last hour
  if( endMinute < (  startMinute + 60 - interval ) ) {     
    expressions.add( String.format( "0 %d-%d/%d %d * * ?", startMinute, endMinute, interval, endHour ) );

    //the main expression needs to run up to the second to last hour
    endHour--;
  }

  //if the main expression would still be 2+ hours in length
  if( startHour < endHour ) {        
    expressions.add( String.format( "0 %d/%d %d-%d * * ?", startMinute, interval, startHour, endHour ) );
  }
  //if the main expression is only 1 hour long don't use x-x
  else if ( startHour == endHour ) {
    expressions.add( String.format( "0 %d/%d %d * * ?", startMinute, interval, startHour ) );
  }

  return expressions;
}

Upvotes: 6

Related Questions