Home > General > Quartz: test a cron expression

Quartz: test a cron expression

quartz logo

quartz logo

Cron expressions in quartz can sometimes be difficult to test, especially when the cron is programmed to trigger after hours or days. Unless you want to wait that time, here is some snippet code you can use to test a cron.

import java.text.ParseException;
import java.util.Date;

import org.quartz.CronExpression;

public class CronTester {

	public static void main(String[] args) throws ParseException {
		final String expression = "0 0 * * * ?";
        final CronExpression cronExpression = new CronExpression(expression);

        final Date nextValidDate1 = cronExpression.getNextValidTimeAfter(new Date());
        final Date nextValidDate2 = cronExpression.getNextValidTimeAfter(nextValidDate1);

        System.out.println(nextValidDate1);
        System.out.println(nextValidDate2);

	}
}

Outputs:

Tue Aug 18 14:00:00 CEST 2009
Tue Aug 18 15:00:00 CEST 2009
Bookmark and Share
Categories: General Tags: ,
  1. February 3rd, 2010 at 14:44 | #1

    Very nice article. Helpfull to me.

  2. Brij
    March 10th, 2010 at 05:35 | #2

    Thanks..Really helpful

  1. No trackbacks yet.