java 判断当前时间符合cron时间表达式



public class OrderExceptionQuartz {

    private static Logger logger = LoggerFactory.getLogger(OrderExceptionQuartz.class);
    /**
     * 校验在当前时间是否满足cron时间规则表达式
     * @param cron
     * @param format
     * @return
     * @throws ParseException
     */
    private static Boolean filterWithCronTime(String cron, String format) throws ParseException {
        if (StringUtils.isBlank(cron) || StringUtils.isBlank(format)){
            return false;
        }
        CronExpression exp = new CronExpression(cron);
        Boolean inCron = exp.isSatisfiedBy(DateUtils.dateStrToDate(DateUtils.formatCurrentDate(format), format)) ;
        return inCron;
    }

    public static void main(String[] args) throws ParseException {
        String cron = "0 0 10,11,15 * * ? ";
        System.out.println(filterWithCronTime(cron, "HH"));//true,我当前时间为15:36,
        System.out.println(filterWithCronTime(cron, "HHmm"));//false,我当前时间为15:36,
    }

}


你可能感兴趣的:(java 判断当前时间符合cron时间表达式)