cron表达式的生成与解析


/**
 * Cron表达式工具类:目前支持三种常用的cron表达式
 * 1.每天的某个时间点执行      例:12 12 12 * * ?表示每天12时12分12秒执行
 * 2.每周的哪几天执行         例:12 12 12 ? * 1,2,3表示每周的周1周2周3 ,12时12分12秒执行
 * 3.每月的哪几天执行         例:12 12 12 1,21,13 * ?表示每月的1号21号13号 12时12分12秒执行
 *
 * @Author 法哈席基
 */
public class CronUtil {

    /**
     * 方法摘要:构建Cron表达式
     *
     * @param jobScheduleModel
     * @return String
     */
    public static String createCronExpression(JobScheduleModel jobScheduleModel) {
        StringBuffer cronExp = new StringBuffer("");

        if (null == jobScheduleModel.getJobType()) {
            System.out.println("执行周期未配置");
        }

        if (null != jobScheduleModel.getSecond()
                && null != jobScheduleModel.getMinute()
                && null != jobScheduleModel.getHour()) {
            //
            cronExp.append(jobScheduleModel.getSecond()).append(" ");
            //
            cronExp.append(jobScheduleModel.getMinute()).append(" ");
            // 小时
            cronExp.append(jobScheduleModel.getHour()).append(" ");

            // 每天
            if (jobScheduleModel.getJobType().intValue() == JobConstants.JOB_TYPE_DAY) {
                //
                cronExp.append("* ")
                        //
                        .append("* ")
                        //
                        .append("?");
            }

            // 按每周
            else if (jobScheduleModel.getJobType().intValue() == JobConstants.JOB_TYPE_WEEK) {
                // 一个月中第几天
                cronExp.append("? ")
                // 月份
                .append("* ");
                //
                Integer[] weeks = jobScheduleModel.getDayOfWeeks();
                for (int i = 0; i < weeks.length; i++) {
                    if (i == 0) {
                        cronExp.append(weeks[i]);
                    } else {
                        cronExp.append(",").append(weeks[i]);
                    }
                }

            }

            // 按每月
            else if (jobScheduleModel.getJobType().intValue() == JobConstants.JOB_TYPE_MONTH) {
                // 一个月中的哪几天
                Integer[] days = jobScheduleModel.getDayOfMonths();
                for (int i = 0; i < days.length; i++) {
                    if (i == 0) {
                        cronExp.append(days[i]);
                    } else {
                        cronExp.append(",").append(days[i]);
                    }
                }
                // 月份
                cronExp.append(" * ")
                //
                .append("?");
            }

        } else {
            // 时或分或秒参数未配置
            System.out.println("时或分或秒参数未配置");
        }
        return cronExp.toString();
    }

    /**
     * 方法摘要:生成计划的详细描述
     *
     * @param jobScheduleModel
     * @return String
     */
    public static String createDescription(JobScheduleModel jobScheduleModel) {
        StringBuffer description = new StringBuffer("");
        // 计划执行开始时间
//      Date startTime = taskScheduleModel.getScheduleStartTime();

        if (null != jobScheduleModel.getSecond()
                && null != jobScheduleModel.getMinute()
                && null != jobScheduleModel.getHour()) {
            // 按每天
            if (jobScheduleModel.getJobType().intValue() == 1) {
                description.append("每天");
                description.append(jobScheduleModel.getHour()).append("时");
                description.append(jobScheduleModel.getMinute()).append("分");
                description.append(jobScheduleModel.getSecond()).append("秒");
                description.append("执行");
            }

            // 按每周
            else if (jobScheduleModel.getJobType().intValue() == 2) {
                if (jobScheduleModel.getDayOfWeeks() != null && jobScheduleModel.getDayOfWeeks().length > 0) {
                    String days = "";
                    for (int i : jobScheduleModel.getDayOfWeeks()) {
                        days += "周" + i;
                    }
                    description.append("每周的").append(days).append(" ");
                }
                if (null != jobScheduleModel.getSecond()
                        && null != jobScheduleModel.getMinute()
                        && null != jobScheduleModel.getHour()) {
                    description.append(",");
                    description.append(jobScheduleModel.getHour()).append("时");
                    description.append(jobScheduleModel.getMinute()).append("分");
                    description.append(jobScheduleModel.getSecond()).append("秒");
                }
                description.append("执行");
            }

            // 按每月
            else if (jobScheduleModel.getJobType().intValue() == 3) {
                // 选择月份
                if (jobScheduleModel.getDayOfMonths() != null && jobScheduleModel.getDayOfMonths().length > 0) {
                    String days = "";
                    for (int i : jobScheduleModel.getDayOfMonths()) {
                        days += i + "号";
                    }
                    description.append("每月的").append(days).append(" ");
                }
                description.append(jobScheduleModel.getHour()).append("时");
                description.append(jobScheduleModel.getMinute()).append("分");
                description.append(jobScheduleModel.getSecond()).append("秒");
                description.append("执行");
            }

        }
        return description.toString();
    }

    // demo
    public static void main(String[] args) {
        // 执行时间:每天的12时12分12秒 start
        JobScheduleModel jobScheduleModel = new JobScheduleModel();
        jobScheduleModel.setJobType(1);// 按每天
        Integer hour = 12; // 
        Integer minute = 12; // 
        Integer second = 12; // 
        jobScheduleModel.setHour(hour);
        jobScheduleModel.setMinute(minute);
        jobScheduleModel.setSecond(second);
        String cropExp = createCronExpression(jobScheduleModel);
        System.out.println(cropExp + ":" + createDescription(jobScheduleModel));
        // 执行时间:每天的12时12分12秒 end

        jobScheduleModel.setJobType(2);// 每周的哪几天执行
        Integer[] dayOfWeeks = new Integer[3];
        dayOfWeeks[0] = 1;
        dayOfWeeks[1] = 2;
        dayOfWeeks[2] = 3;
        jobScheduleModel.setDayOfWeeks(dayOfWeeks);
        cropExp = createCronExpression(jobScheduleModel);
        System.out.println(cropExp + ":" + createDescription(jobScheduleModel));

        jobScheduleModel.setJobType(3);// 每月的哪几天执行
        Integer[] dayOfMonths = new Integer[3];
        dayOfMonths[0] = 1;
        dayOfMonths[1] = 21;
        dayOfMonths[2] = 13;
        jobScheduleModel.setDayOfMonths(dayOfMonths);
        cropExp = createCronExpression(jobScheduleModel);
        System.out.println(cropExp + ":" + createDescription(jobScheduleModel));

    }
}
import lombok.Data;

/**
 * 任务调度实体
 *
 * @Author 法哈席基
 */
@Data
public class JobScheduleModel {
    /**
     * 执行周期类型:
     * 1  -> 每天
     * 2  -> 每周
     * 3  -> 每月
     * 4  ->间隔(每隔2个小时,每隔30分钟)
     */
    private Integer jobType;

    /**
     * 一周的哪几天
     */
    private Integer[] dayOfWeeks;

    /**
     * 一个月的哪几天
     */
    private Integer[] dayOfMonths;

    /**
     * 秒
     */
    private Integer second;

    /**
     * 分
     */
    private Integer minute;

    /**
     * 时
     */
    private Integer hour;
}
/**
* 任务常量
* * @Author 法哈席基 */ public interface JobConstants { /** * 【执行周期】1:每日、2:每周、3:每月 */ int JOB_TYPE_DAY = 1; int JOB_TYPE_WEEK = 2; int JOB_TYPE_MONTH = 3; }