JavaBlog.fr / Java.lu DEVELOPMENT,Java Java: Quartz Cron Expression checker

Java: Quartz Cron Expression checker

A mini-post to expose a simple method to check the Quartz cron expression:

public static void testCronExpression(String expression) throws Throwable  {
	CronExpression cronExpression = new CronExpression(expression);
	Date d1 = cronExpression.getNextValidTimeAfter(new Date());
	int i=0;
	while(i<=100) {
		System.out.println(d1);
		d1 = cronExpression.getNextValidTimeAfter(d1);
		i++;
	}
}

Example 1:

testCronExpression("0 0/5 8-19 ? * 2,3,4,5,6");

The result in the console could be:

Thu Aug 16 10:55:00 CEST 2012
Thu Aug 16 11:00:00 CEST 2012
Thu Aug 16 11:05:00 CEST 2012
Thu Aug 16 11:10:00 CEST 2012
Thu Aug 16 11:15:00 CEST 2012
Thu Aug 16 11:20:00 CEST 2012
...
Fri Aug 17 15:30:00 CEST 2012
Fri Aug 17 15:35:00 CEST 2012

Example 2:

testCronExpression("0 32 10,13,16 ? * MON,TUE,WED,THU");

The result in the console could be:

Thu Aug 16 13:32:00 CEST 2012
Thu Aug 16 16:32:00 CEST 2012
Mon Aug 20 10:32:00 CEST 2012
Mon Aug 20 13:32:00 CEST 2012
Mon Aug 20 16:32:00 CEST 2012
...
Thu Oct 11 16:32:00 CEST 2012
Mon Oct 15 10:32:00 CEST 2012
Mon Oct 15 13:32:00 CEST 2012
Mon Oct 15 16:32:00 CEST 2012

Example 3:

testCronExpression("0 0/5 7-19 ? * 2,3,4,5,6");

The result in the console could be:

Thu Aug 16 11:05:00 CEST 2012
Thu Aug 16 11:10:00 CEST 2012
Thu Aug 16 11:15:00 CEST 2012
Thu Aug 16 11:20:00 CEST 2012
Thu Aug 16 11:25:00 CEST 2012
Thu Aug 16 11:30:00 CEST 2012
...
Thu Aug 16 18:55:00 CEST 2012
Thu Aug 16 19:00:00 CEST 2012
Thu Aug 16 19:05:00 CEST 2012
Thu Aug 16 19:10:00 CEST 2012
Thu Aug 16 19:15:00 CEST 2012
Thu Aug 16 19:20:00 CEST 2012
Thu Aug 16 19:25:00 CEST 2012

Note: This method uses the quartz-1.6.0.jar version.

That’s all!!!

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post