Quartz CronTrigger - Getting next fire time

55,999

Solution 1

What you really want to use the the CronExpression object directly not the CronTrigger. As you discovered, it won't calculate next run times in the past... but CronExpression will!

CronExpression has the method: getNextValidTimeAfter. This is what you want.

Solution 2

In Spring you can follow the same approach they use in their integration tests. CronTriggerTests

CronTrigger trigger = new CronTrigger();
trigger.setCronExpression("0 0 23 3,18 * ? *");
SimpleTriggerContext triggerContext = new SimpleTriggerContext();
triggerContext.update(null, null, new Date());
Date nextFireAt = trigger.nextExecutionTime(triggerContext);

Solution 3

The CronTrigger.getFireTimeAfter() method has a protection mechanism for preventing the specified after time to be in the past.

This can be circumvented by calling setStartTime firth with the time in the past you are evaluating. Then the call to getFireTimeAfter should return with a valid result.

Share:
55,999

Related videos on Youtube

cambo
Author by

cambo

Updated on May 24, 2020

Comments

  • cambo
    cambo almost 4 years

    I'm using the Quartz CronTrigger facility to parse cron schedule format strings to determine when a specific job should run. I'm not actually using Quartz to schedule the job though.

    There is a method in CronTrigger called getFireTimeAfter(Date) which gives the next time the job will fire after the given date. This works well when the supplied date is now or in the future. But it doesn't seem to work if the date is in the past.

    Date currTime = new Date();
    CronTrigger tr = new CronTrigger();
    tr.setCronExpression("0 0 23 3,18 * ? *");
    Date nextFireAt = tr.getFireTimeAfter(currTime);
    System.out.println("Reference time: " + currTime);
    System.out.println("Next fire after reference time: " + nextFireAt);
    

    Which is a cron schedule to fire at 23:00 on the 3 and 18 of every month. So for example, if I did this today (August 11), I see:

    Reference time: Thu Aug 11 10:04:25 MDT 2011
    Next fire after reference time: Thu Aug 18 23:00:00 MDT 2011
    

    But if I set the reference date to the past, it gives me the same next fire time.

    Reference time: Wed Dec 31 17:00:00 MST 1969
    Next fire after reference time: Thu Aug 18 23:00:00 MDT 2011
    

    I was expecting the output to be:

    Reference time: Wed Dec 31 17:00:00 MST 1969
    Next fire after reference time: Wed Aug 3 23:00:00 MDT 2011
    

    Is the method just not intended to work that way or am I doing something wrong?

    Thanks!

    • cambo
      cambo over 12 years
      @Kal - yes I know. I am trying to figure out when the trigger would have fired after the given date, even if it is in the past.
  • cambo
    cambo over 12 years
    I'm trying to use the CronTrigger to know if a certain "due date" has happened. The two pieces of information I have are the "last due date time" and the cron schedule. So what I want to do is getFireTimeAfter(lastDueDate) to tell me if whether I am past the current due date. Looks like getTimeBefore() may be what I'm looking for -- too bad it's not implemented yet.
  • Manik Arora
    Manik Arora about 8 years
    Is there a way to find all occurences within a datetime range?
  • cvacca
    cvacca over 7 years
    @ManikArora : You can do a while loop calling getNextValidTimeAfter passing as parameter first the beginning datetime, and then all returned timestamps on each iteration, and you loop while those timestamps are still less than the ending time. On each iteration you can add the resulting Dates to the list of occurrences.