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
This entry was posted in General and tagged , . Bookmark the permalink.

5 Responses to Quartz: test a cron expression

  1. daniel says:

    Very nice article. Helpfull to me.

  2. Brij says:

    Thanks..Really helpful

  3. Woterloo says:

    Many Thanks.
    Very handy.

  4. yd says:

    Thanks for the class.
    A small modification for testing:


    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 = "25 1/2 * * * ?";
    final CronExpression cronExpression = new CronExpression(expression);
    Date date = new Date();
    for(int i=0; i<20;i++) {
    date = cronExpression.getNextValidTimeAfter(date);
    System.out.println(date);
    }
    }
    }

  5. Ravi says:

    Hi
    Can anybody help me writing a Cron Expression.To schedule jobs on 11.30 am and 2.45pm within a single Cron Expression.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">