Is there a way in Java to find the "Last Fired Time" from a Cron Expression?
E.g. If now = 25-Apr-2010 10PM
, and the cron expression is 0 15 10 ? * *
(quartz), it should return 25-Apr-2010 10:15AM
.
Note:
cron-utils is an opensource Java library to parse, validate, migrate crons that supports the operation you need. To get the previous date from a cron before a given time simply:
//Get date for last execution
DateTime now = DateTime.now();
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * * * *"));
DateTime lastExecution = executionTime.lastExecution(now));
Bear in mind that in its current state it is a bit buggy and may not compute correctly for more complex cron expressions.