前端不想像core表达式官网:https://cron.qqe2.com/这样,有一定的学习成本。
然后这个工具类的实现的效果就是把0 23 * * *这种,
翻译为每1天执行。指定时:23。指定分:00。
/**
* cron转换中文工具类
*
* @author lixuan
*/
public class CronUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(CronUtil.class);
/**
* cron中文表达式
*/
private static final List<ValueLabelPair> HOUR_LIST = generateValueLabelPairs(0, 23);
private static final List<ValueLabelPair> MINUTES_LIST = generateValueLabelPairs(0, 59);
private static final List<ValueLabelPair> DATE_LIST = generateDateValueLabelPairs(1, 31);
public static String convertToChineseExpression(String cronExpression) {
String result = "";
try {
String[] arr = cronExpression.split(" ");
int index = -1;
for (int i = 0; i < arr.length; i++) {
if ("*".equals(arr[i]) || arr[i].contains("/")) {
index = i;
break;
}
}
int n;
String unit;
if ("*".equals(arr[index])) {
n = 1;
} else {
n = Integer.parseInt(arr[index].split("/")[1]);
}
switch (index) {
case 0:
unit = "分";
break;
case 1:
unit = "时";
break;
case 2:
unit = "天";
break;
case 3:
unit = "月";
break;
case 4:
unit = "星期";
break;
default:
unit = "";
break;
}
result = "每" + n + unit + "执行。";
if ("月".equals(unit) || "天".equals(unit)) {
String specifiedDays = expandDayAsSpecifiedValues(arr[2], DATE_LIST);
//排序
specifiedDays = updateSpecified(specifiedDays);
if (!"".equals(specifiedDays) && !arr[2].contains("/")) {
result += "指定日:" + specifiedDays + "。";
}
}
if (!"分".equals(unit) && !"时".equals(unit)) {
String specifiedHours = expandSpecifiedValues(arr[1], HOUR_LIST);
//排序
specifiedHours = updateSpecified(specifiedHours);
result += "指定时:" + specifiedHours + "。";
}
if (!"分".equals(unit)) {
String specifiedMinutes