把Cron表达式 转换成中文,提供给前端展示执行间隔。


    private static final String QUESTION_MARK = "?";
    private static final String STAR = "*";
    private static final int NORMAL_CRON_LENGTH = 6;
    private final static String ZERO = "00";
    private final static String SLASH = "/";
    private final static int ZEROS = 0;
    private final static int ONE = 1;
    private final static int TWO = 2;
    private final static int THREE = 3;
    private final static int FOUR = 4;
    private final static int FIVE = 5;


    public static String translateToChinese(String cronExp) {
        if (cronExp == null || cronExp.length() < 1) {
            return "未配置触发时间";
        }
        CronExpression exp = null;
        // 初始化cron表达式解析器
        try {
            exp = new CronExpression(cronExp);
        } catch (ParseException e) {
            return "corn表达式不正确";
        }
        String[] tmpCorns = cronExp.split(" ");
        StringBuilder sBuffer = new StringBuilder();
        if (tmpCorns.length == NORMAL_CRON_LENGTH) {
            // 解析月
            if (!STAR.equals(tmpCorns[FOUR])) {
                sBuffer.append(tmpCorns[FOUR]).append("月");
            } else {
                if (!STAR.equals(tmpCorns[THREE])) {
                    sBuffer.append("每月");
                }
                if ((!STAR.equals(tmpCorns[FIVE]) && !QUESTION_MARK.equals(tmpCorns[FIVE]))) {
                    sBuffer.append("每月");
                }
            }
            // 解析周
            if (!STAR.equals(tmpCorns[FIVE]) && !QUESTION_MARK.equals(tmpCorns[FIVE])) {
                char[] tmpArray = tmpCorns[FIVE].toCharArray();
                for (char tmp : tmpArray) {
                    switch (tmp) {
                    case '1':
                        sBuffer.append("星期天");
                        break;
                    case '2':
                        sBuffer.append("星期一");
                        break;
                    case '3':
                        sBuffer.append("星期二");
                        break;
                    case '4':
                        sBuffer.append("星期三");
                        break;
                    case '5':
                        sBuffer.append("星期四");
                        break;
                    case '6':
                        sBuffer.append("星期五");
                        break;
                    case '7':
                        sBuffer.append("星期六");
                        break;
                    case '-':
                        sBuffer.append("至");
                        break;
                    default:
                        sBuffer.append(tmp);
                        break;
                    }
                }
            }

            // 解析日
            if (!QUESTION_MARK.equals(tmpCorns[THREE])) {
                if (!STAR.equals(tmpCorns[THREE])) {
                    if (tmpCorns[THREE].contains(SLASH)) {
                        String[] dayCorn = tmpCorns[THREE].split(SLASH);
                        if (STAR.equals(dayCorn[ZEROS])) {
                            sBuffer.append("每").append(dayCorn[ONE]).append("天");
                        } else {
                            sBuffer.append(" 每月从").append(dayCorn[ZEROS]).append("号开始 每").append(dayCorn[ONE]).append("天");
                        }
                    } else {
                        sBuffer.append(tmpCorns[THREE]).append("分");
                    }
                } else {
                    sBuffer.append("每天");
                }
            }

            // 解析时
            if (!STAR.equals(tmpCorns[TWO])) {
                if (tmpCorns[TWO].contains(SLASH)) {
                    String[] hourCorn = tmpCorns[TWO].split(SLASH);
                    if (STAR.equals(hourCorn[ZEROS])) {
                        sBuffer.append("每").append(hourCorn[ONE]).append("小时");
                    } else {
                        sBuffer.append("从").append(hourCorn[ZEROS]).append("点开始 每").append(hourCorn[ONE]).append("小时");
                    }
                } else {
                    sBuffer.append(tmpCorns[TWO]).append("分");
                }
            } else {
                sBuffer.append("每小时");
            }

            // 解析分
            if (!STAR.equals(tmpCorns[ONE])) {
                if (!(ZERO.equals(tmpCorns[ONE]) && ZERO.equals(tmpCorns[ZEROS]))) {
                    if (tmpCorns[ONE].contains(SLASH)) {
                        String[] minuteCorn = tmpCorns[ONE].split(SLASH);
                        if (STAR.equals(minuteCorn[ZEROS])) {
                            sBuffer.append("每").append(minuteCorn[ONE]).append("分");
                        } else {
                            sBuffer.append(" 每小时从").append(minuteCorn[ZEROS]).append("分开始 每").append(minuteCorn[ONE]).append("分");
                        }
                    } else {
                        sBuffer.append(tmpCorns[ONE]).append("分");
                    }
                }
            } else {
                sBuffer.append("每分");
            }

            // 解析秒
            if (!STAR.equals(tmpCorns[ZEROS])) {
                if (!(ZERO.equals(tmpCorns[ZEROS]))) {
                    if (tmpCorns[ZEROS].contains(SLASH)) {
                        String[] secondCorn = tmpCorns[ZEROS].split(SLASH);
                        if (STAR.equals(secondCorn[ZEROS])) {
                            sBuffer.append("每").append(secondCorn[ONE]).append("秒");
                        } else {
                            sBuffer.append(" 每分钟从").append(secondCorn[ZEROS]).append("秒开始 每").append(secondCorn[ONE]).append("秒");
                        }
                    } else {
                        sBuffer.append(tmpCorns[ZEROS]).append("分");
                    }
                }
            } else {
                sBuffer.append("每秒");
            }
        }

        return sBuffer.append("执行").toString();

    }

 

你可能感兴趣的:(cron,cron,java)