Gogi
Gogi

Reputation: 1695

How to find if a given timestamp is within a cron-expression?

I am using the quartz scheduler to schedule some jobs in Java using cron expressions, and have come across the need to find you whether a given timestamp is within a specified cron expression. That is to say, would a scheduled job with this cron run at the given timestamp?

I am not confined to using quartz library to solve this problem, so any solution is welcome.

Upvotes: 4

Views: 3456

Answers (2)

zerologiko
zerologiko

Reputation: 2111

The Calendar interface has just the method you need to check if a timestamp is "included" in the Calendar and thus the CronTrigger will fire with the given timestamp:

boolean includedisTimeIncluded(long timeStamp)
  • You can take the Calendar name from the CronTrigger using the Inherited Trigger method:

    String calendarName = myTrigger.getCalendarName();
    
  • Then retrieve the Calendar from the Scheduler instance and check your timestamp:

    Calendar cal = sched.getCalendar(calendarName);
    boolean willFire = cal.includedisTimeIncluded(timeStampToCheck);
    

Upvotes: 0

Matthias
Matthias

Reputation: 43

There is

Date getFireTimeAfter(Date afterTime)

In the trigger interface.

You could check what the next firetime t-1 is when t is the time you want to Check. If the result equals t you know the answer.

Upvotes: 1

Related Questions