Interview Questions

How to validate your cron expressions for Spring Scheduler ?

Use the following code to validate your cron expressions. The following code prints the date/time when the scheduler will be triggered based on two different cron expressions.

public static void main(String[] args)
 {
  CronSequenceGenerator cron1 = new CronSequenceGenerator("0 0 1 * * ? ");
  CronSequenceGenerator cron2 = new CronSequenceGenerator("0 0 1 1 * ? ");
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.DATE, 2); // add two days to current date
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
 
  System.out.println("current date "+sdf.format(cal.getTime()));
 
  System.out.println("Next cron trigger date cron1 "+cron1.next(cal.getTime()));
  System.out.println("Next cron trigger date cron2 "+cron2.next(cal.getTime()));
 }

2
Average: 2 (2 votes)
Your rating: None