我想确保我的Cron表达式每周六凌晨1点运行一次。我编写了以下代码来验证它,但看起来每分钟会触发一次:
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);
}
}
}
该程序的输出是:
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
任何想法Cron表达式的错误以及正确的表达方式是什么?
感谢。
答案 0 :(得分:0)
咄!我使用了错误的方法(getNextInvalidTimeAfter
而不是getNextValidTimeAfter
)。