public class CronExprUtil {
private CronExprUtil() {
throw new IllegalStateException("CronExprUtil class");
}
/**
* @param cron cron 表达式
* @Description: 校验cron 表达式格式是否正确
* @return: true/false
*/
public static boolean isCronRight(String cron) {
if (StringUtils.isBlank(cron)) {
return false;
}
try {
new CronExpression(cron);
} catch (ParseException e) {
return false;
}
return true;
}
/**
* @param cron cron表达式
* @return List<String>
*/
public static List<String> mockTrigger(String cron) throws ParseException {
List<String> rtn = new ArrayList<>(10);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CronExpression cronExpr = new CronExpression(cron);
// 初始化上次执触发时间为当前时间
Date now = new Date();
for (int i = 0; i < 10; i++) {
Date nextInvokeTime = cronExpr.getNextValidTimeAfter(now);
rtn.add(sdf.format(nextInvokeTime));
now = cronExpr.getNextInvalidTimeAfter(nextInvokeTime);
}
return rtn;
}
public static void main(String[] args) throws ParseException {
String cron = "0 0 18 L * ?";
if (isCronRight(cron)) {
mockTrigger(cron).forEach(System.out::println);
}
}
}
08-17
05-06
1284
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交