Parsing CRON expression of spring combat code

Parsing CRON expression of spring combat code

If we want to use spring's timer function, we usually use CRON expressions. In fact, the CRON analysis implemented by each framework is also different, so we need to use the corresponding analysis rules:

import org.springframework.scheduling.support.CronSequenceGenerator;

try {
	int size = 10;
	// 每月1号执行
	String cron = "0 0 0 1 1/1 ?";
	final CronSequenceGenerator g = new CronSequenceGenerator(cron);
	Date d = new Date();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	List<String> res = new ArrayList<>(size);
	for (int i = 0; i < size; i++) {
		d = g.next(d);
		res.add(sdf.format(d));
	}
	res.forEach(System.out::println);
} catch (Exception e) {
	e.printStackTrace();
}

The above code returns 10 times that match the expression:

2020-05-01 00:00:00
2020-06-01 00:00:00
2020-07-01 00:00:00
2020-08-01 00:00:00
2020-09-01 00:00:00
2020-10-01 00:00:00
2020-11-01 00:00:00
2020-12-01 00:00:00
2021-01-01 00:00:00
2021-02-01 00:00:00
Published 80 original articles · Like 319 · Visits 340,000+

Guess you like

Origin blog.csdn.net/jimo_lonely/article/details/105418387