views:

238

answers:

2

Is there a way in Java to find the "Last Fired Time" from a Cron Expression.

E.g. If now = 25-Apr-2010 10pm, cron expression "0 15 10 ? * *" (quartz) should return me 25-Apr-2010 10:15am

Note: 1) I do not care if we use standard cron expressions (like Unix and Quartz) or less popular ones if they can fetch me the correct "Last Fired Time" 2) Also it is not literally "Last Fire time" as the trigger may not have fired, but logically there should be a way of telling when it (would have) fired last.

+1  A: 

First, I am not aware of an existing library that supports this. Quartz might, but the standard Java class libraries certainly don't.

Second, strictly speaking what you are asking for originally asked for is impossible. The best that a library can tell you is that the cron expression would have or should have fired. The only thing that could (theoretically) tell you the last time that a cron expression actually fired is the scheduler instance itself.

Stephen C
Hi Stephen, Thanks for your feedback. I have added a little more info in notes of my question to clear my problem.
a-sak
+1  A: 

Quartz seems to have some library support for cron expressions.

See the Javadoc for the CronExpression class, which has a method called getTimeBefore. I.e.,

CronExpression cron = new CronExpression("0 15 10 ? * *");
Date today = new Date();
Date previousExecution = cron.getTimeBefore(today);

It might depend on the Quartz version whether this works. In another Javadoc reference it claimed this method was not yet implemented, so you will have experiment.

waxwing