cron expression parsing into java date

user3599212 picture user3599212 · May 23, 2014 · Viewed 26.9k times · Source
  • my database having 10 18 16 ? * SUN,MON,WED,FRI * cron expression then how to convert into Java date.
  • how to comparing with present day time.
  • and one more is how to compare to cron expressions i.e. 10 18 16 ? * SUN,MON,WED,FRI * and 0 30 9 30 * ?
  • please explain the sample code using quartz or spring scheduling.

Answer

Matt picture Matt · Dec 15, 2015

Please use:

import org.springframework.scheduling.support.CronSequenceGenerator;

final String cronExpression = "0 45 23 * * *";
final CronSequenceGenerator generator = new CronSequenceGenerator(cronExpression);
final Date nextExecutionDate = generator.next(new Date());

...and then I suggest use Joda DateTime for date comparison.