提交修改
This commit is contained in:
parent
59a6a9354f
commit
838612adf3
@ -0,0 +1,90 @@
|
||||
package com.heyu.api.schedule;
|
||||
|
||||
import com.heyu.api.data.utils.DateUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.TriggerUtils;
|
||||
import org.quartz.impl.triggers.CronTriggerImpl;
|
||||
import org.springframework.scheduling.support.CronSequenceGenerator;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class CronTriggerUtils {
|
||||
|
||||
|
||||
/**
|
||||
* @param cronExpression cron表达式
|
||||
* @param numTimes 下一(几)次运行的时间
|
||||
* @return
|
||||
*/
|
||||
public static Date getNextExecTime(String cronExpression) {
|
||||
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
|
||||
try {
|
||||
cronTriggerImpl.setCronExpression(cronExpression);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 这个是重点,一行代码搞定
|
||||
List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, 1);
|
||||
for (Date date : dates) {
|
||||
return date;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param cronExpression cron表达式
|
||||
* @param numTimes 下一(几)次运行的时间
|
||||
* @return
|
||||
*/
|
||||
public static Date getCurrentExecTime(String cronExpression) {
|
||||
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
|
||||
Date lastMinute= DateUtils.getBeforeMinute(new Date(),1);
|
||||
Date nextTriggerTime = cronSequenceGenerator.next(lastMinute);//lastMinute 我这里是上一分钟的date类型对象
|
||||
return nextTriggerTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cronExpression cron表达式
|
||||
* @param numTimes 下一(几)次运行的时间
|
||||
* @return
|
||||
*/
|
||||
public static List<String> getNextsExecTime(String cronExpression, Integer numTimes) {
|
||||
List<String> list = new ArrayList<>();
|
||||
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
|
||||
try {
|
||||
cronTriggerImpl.setCronExpression(cronExpression);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 这个是重点,一行代码搞定
|
||||
List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, numTimes);
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
for (Date date : dates) {
|
||||
list.add(dateFormat.format(date));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String cronExpression = "0 * * * * ?";
|
||||
Date nextTunTime = CronTriggerUtils.getCurrentExecTime(cronExpression);
|
||||
System.out.println(nextTunTime);
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
|
||||
for (int i = 0; i < 10; i++){
|
||||
log.info("执行时间:" + df.format(nextTunTime));
|
||||
Date nextTriggerTime = cronSequenceGenerator.next(nextTunTime);//lastMinute 我这里是上一分钟的date类型对象
|
||||
nextTunTime = nextTriggerTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user