6

Execute the job on Monday until Saturday from 7pm until 9am and the whole day for Sunday.

I try to input multiple expressions of cron, but it's not working. Can anyone get me the solution for this?

1. " * * 19-8 ? * MON,TUE,WED,THU,FRI,SAT "
2. " * * * ? * SUN "
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Fatin Az
  • 509
  • 2
  • 6
  • 8

5 Answers5

5

Since you are using Quartz, you can create several different CronTriggers, and schedule all of them to your required job. E.g.(change the cron expressions to the expressions that you need)

SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

JobDetail job = newJob(SimpleJob.class)
    .withIdentity("job1", "group1")
    .build();

Set<Trigger> triggers = new HashSet<>();

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?"))
    .build();
triggers.add(trigger1);

CronTrigger trigger2 = newTrigger()
    .withIdentity("trigger2", "group1")
    .withSchedule(cronSchedule("15 0/2 * * * ?"))
    .build();
triggers.add(trigger2);

CronTrigger trigger3 = newTrigger()
    .withIdentity("trigger3", "group1")
    .withSchedule(cronSchedule("0 0/2 8-17 * * ?"))
    .build();
triggers.add(trigger3);

scheduler.scheduleJob(job, triggers, false);

You can't create one trigger with multiple CronExpressions.

yishaiz
  • 2,433
  • 4
  • 28
  • 49
1

If one needs for nodejs, https://github.com/datasert/cronjs does multi cron expressions. For ex., * 21-23 * * 2,4,6|* 0-5 * * 1,3,5|* * ? * 7

Disclaimer I'm part of the team which builds that library

metasync
  • 338
  • 1
  • 10
0

I think seeing complexity of your requirement we need to create 4 cron expression for your task to complete.

// task for Monday 7 PM to 12 PM

==>

 * 19-24 * * 1  <YOUR_TASK>
    ->* – every Minute 
    ->19-24 hours
    ->* – Every day
    ->* – Every month
    ->1--Mon,

//TASK for Tuesday to Friday ==>

* 00-24 * * 2-5  <YOUR_TASK>
->* – 0th Minute 
-> 00-24 hours
->* – Every day
->* – Every month
->1-5 -Mon, Tue, Wed, Thu , Fri, Sat

//task for Saturday upto 9 AM ==>

* 00-09 * * 6  <YOUR_TASK>
->00 – every Minute 
->00-09 – upto 9 AM
->* – Every day
->* – Every month
->6 -, Sat

//task for Saturday ==>

 * * * * 7   <YOUR_TASK>
    ->* – Every minute  
    ->00-09 – upto 9 AM
    ->* – Every day
    ->* – Every month
    ->6 -, Sat
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
0

CronMaker is a utility which helps you to build cron expressions. CronMaker uses Quartz open source scheduler. Generated expressions are based on Quartz cron format.

This expressions defines the start of a task. It does not define its duration (it belongs to the task).

- used to specify ranges. For example, "10-12" in the hour field means "the hours 10, 11 and 12"

CronTrigger Tutorial

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

cron-utils introduced a multi-cron notation: you can combine multiple crons into a single expression. Below an example:

String multicron = "0 0|0|30|0 9|10|11|12 * * ? *";
parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
Cron cron = parser.parse(multicron);
assertEquals(multicron, cron.asString());

The cron notation is the same as for any regular cron: the fields that hold the same values across the crons, remain the same. The fields that would hold different values, will have them separated by pipes.

Your two crons could be expressed as:

"* * 19-8|* ? * MON,TUE,WED,THU,FRI,SAT|SUN"

Currently cron-utils does not support jobs execution, but provides means to know next/previous execution date.

sashimi
  • 1,224
  • 2
  • 15
  • 23