TheFooProgrammer
TheFooProgrammer

Reputation: 2507

Why the following cron expression runs everything, as opposed to once every Saturday at 1 AM?

I wanted to make sure that my Cron expressions only runs once every Saturday at 1 AM. I have written the following code to verify it, but looks like it will be triggered once per minute:

import org.quartz.CronExpression;

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

public class Main {

    public static void main(String[] args) throws ParseException {
        CronExpression exp = new CronExpression("0 0 1 ? * SAT");
        Calendar calendar = Calendar.getInstance();
        calendar.set(2014, Calendar.JANUARY, 1, 1, 0, 0);

        Date date = calendar.getTime();
        for (int day = 1; day < 31; day++) {
            date = exp.getNextInvalidTimeAfter(date);
            System.out.println("date = " + date);
        }
    }
}

The output of that program is:

date = Wed Jan 01 01:00:01 GMT 2014
date = Wed Jan 01 01:00:02 GMT 2014
date = Wed Jan 01 01:00:03 GMT 2014
date = Wed Jan 01 01:00:04 GMT 2014
date = Wed Jan 01 01:00:05 GMT 2014
date = Wed Jan 01 01:00:06 GMT 2014
date = Wed Jan 01 01:00:07 GMT 2014
date = Wed Jan 01 01:00:08 GMT 2014
date = Wed Jan 01 01:00:09 GMT 2014
date = Wed Jan 01 01:00:10 GMT 2014
date = Wed Jan 01 01:00:11 GMT 2014
date = Wed Jan 01 01:00:12 GMT 2014
date = Wed Jan 01 01:00:13 GMT 2014
date = Wed Jan 01 01:00:14 GMT 2014
date = Wed Jan 01 01:00:15 GMT 2014
date = Wed Jan 01 01:00:16 GMT 2014
date = Wed Jan 01 01:00:17 GMT 2014
date = Wed Jan 01 01:00:18 GMT 2014
date = Wed Jan 01 01:00:19 GMT 2014
date = Wed Jan 01 01:00:20 GMT 2014
date = Wed Jan 01 01:00:21 GMT 2014
date = Wed Jan 01 01:00:22 GMT 2014
date = Wed Jan 01 01:00:23 GMT 2014
date = Wed Jan 01 01:00:24 GMT 2014
date = Wed Jan 01 01:00:25 GMT 2014
date = Wed Jan 01 01:00:26 GMT 2014
date = Wed Jan 01 01:00:27 GMT 2014
date = Wed Jan 01 01:00:28 GMT 2014
date = Wed Jan 01 01:00:29 GMT 2014
date = Wed Jan 01 01:00:30 GMT 2014

Any ideas what's wrong with Cron expression and what the correct expression would be?

Thanks.

Upvotes: 0

Views: 199

Answers (1)

TheFooProgrammer
TheFooProgrammer

Reputation: 2507

Duh! I was using the wrong method (getNextInvalidTimeAfter rather than getNextValidTimeAfter).

Upvotes: 0

Related Questions