**
一 cron 表达式相关
例如[0 0 0 1 * ?,59 59 23 L * ?]每月1日凌晨开启,每月月末23点59分59秒结束
先获取每次执行间隔差值,然后模拟一次一次触发,找到应该触发的时间点
**
获取上次执行时间
/*** 上次执行时间* @param cronInfo* @return*/
public static long getLastTriggerTime(String cronInfo){if(!CronExpression.isValidExpression(cronInfo)){return 0;}try {long currTime = System.currentTimeMillis();CronExpression cron = new CronExpression(cronInfo);Date validTimeAfter = cron.getNextValidTimeAfter(new Date());Date validTimeAfter1 = cron.getNextValidTimeAfter(validTimeAfter);//计算出下下次触发 与下次触发的 差值long diff = validTimeAfter1.getTime() - validTimeAfter.getTime();//当前时间 减去两次差值 long startCalcTime = currTime - diff * 2;Date date = new Date(startCalcTime);//当前时间dateDate currTimeDate = new Date();//应该触发的时间Date previousFireTime = null;Date nextFireTime = cron.getNextValidTimeAfter(date); int i = 0;// 迭代计算上一次执行时间while (nextFireTime != null && nextFireTime.before(currTimeDate)) {previousFireTime = nextFireTime;nextFireTime = cron.getNextValidTimeAfter(nextFireTime);if(i++ >= 100){return 0;}}if (previousFireTime != null) {return previousFireTime.getTime();}}catch (Exception e) {e.printStackTrace();return 0;}return 0;
}
/**
* 通过配置信息获取下阶段时间 单位 毫秒
* @param cronExpression cron表达式
* @return
* @throws ParseException
*/
public long getCronNextTimeByTempInfo(String cronExpression){
try {
if(!CronExpression.isValidExpression(cronExpression)){
return 0;
}
CronExpression cron = new CronExpression(cronExpression);
Date validTimeAfter = cron.getNextValidTimeAfter(new Date());
return validTimeAfter.getTime();
}catch (Exception e){e.printStackTrace();}return 0;
}