import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

public static Date getNTimeByCron(String cron) {
try {
//根据cron算出该任务的下次执行时间
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression(cron);

Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
// 把统计的区间段设置为从现在到2年后的今天(主要是为了方法通用考虑,如那些1个月跑一次的任务,如果时间段设置的较短就不足20条)
calendar.add(Calendar.YEAR, 2);
// 这个是重点,一行代码搞定
List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, now, calendar.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nextTime = dateFormat.format(dates.get(0));
Date date=dateFormat.parse(nextTime);
return date;
} catch (Exception e) {
throw new RuntimeException(e);
}
}