Quartz Scheduler Example Programs and Sample Code : Quartz Code Snippets : How-To: Create a Trigger that Executes Every 2 Days
How-To: Create a Trigger that Executes Every 2 Days
For this kind of schedule, you might be tempted to use a CronTrigger. However, if this is truly to be every two days, a CronTrigger will not work. To illustrate this, simply think of how many days are in a typical month (28-31). A cron expression like "0 0 5 2/2 * ?" would give us a trigger that would restart its count at the beginning of every month. This means that we would get subsequent firings on July 30 and August 2, which is an interval of three days, not two.
Likewise, an expression like "0 0 5 1/2 * ?" would end up firing on July 31 and August 1, just one day apart.
Therefore, for this schedule, using a SimpleTrigger or CalendarIntervalTrigger makes sense:
Using SimpleTrigger
Create a SimpleTrigger that executes 3:00 PM tomorrow, and then every 48 hours after that.
Note:  
Be aware that this trigger might not always fire at 3:00 PM, because adding (2 * 24) hours on days when daylight savings time shifts can result in an execution time of 2:00 PM or 4:00 PM, depending upon whether the 3:00 PM time was started during DST or standard time.
trigger = newTrigger()
.withIdentity("trigger3", "group1")
.startAt(tomorrowAt(15, 0, 0) // first fire time 15:00:00 tomorrow
.withSchedule(simpleSchedule()
             .withIntervalInHours(2 * 24)
             // interval is actually set at 48 hours' worth of milliseconds
             .repeatForever())
.build();
Using CalendarIntervalTrigger
trigger = newTrigger()
.withIdentity("trigger3", "group1")
.startAt(tomorrowAt(15, 0, 0) // 15:00:00 tomorrow
.withSchedule(calendarIntervalSchedule()
.withIntervalInDays(2)) // interval is set in calendar days
.build();
Copyright © 2010-2016 Software AG, Darmstadt, Germany.

Product Logo |   Feedback