实现效果
/**
* 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 = expandSpecifiedValues(arr[0], MINUTES_LIST);
//排序
specifiedMinutes = updateSpecified(specifiedMinutes);
result += "指定分:" + specifiedMinutes + "。";
}
return result;
} catch (NumberFormatException | IndexOutOfBoundsException e) {
LOGGER.error("cron表达式有误");
return result;
}
}
private static String updateSpecified(String specifiedDays) {
String specified = "";
if (StringUtils.isNotBlank(specifiedDays)) {
List<String> list = new ArrayList<>(List.of(specifiedDays.split("、")));
Collections.sort(list);
specified = String.join("、", list);
}
return specified;
}
private static String expandSpecifiedValues(String value, List<ValueLabelPair> list) {
if ("*".equals(value)) {
return "";
} else if (value.contains(",")) {
String[] values = value.split(",");
StringBuilder sb = new StringBuilder();
for (String v : values) {
if (v.contains("/")) {
String[] range = v.split("/");
int start = Integer.parseInt(range[0]);
int step = Integer.parseInt(range[1]);
for (int i = start; i < list.size(); i += step) {
sb.append(list.get(i).getLabel()).append("、");
}
} else if (v.contains("-")) {
String[] range = v.split("-");
int start = Integer.parseInt(range[0]);
int end = Integer.parseInt(range[1]);
for (int i = start; i <= end; i++) {
sb.append(list.get(i).getLabel()).append("、");
}
} else if ("L".equals(v)) {
sb.append(list.get(list.size() - 1).getLabel()).append("、");
} else {
int index = Integer.parseInt(v);
sb.append(list.get(index).getLabel()).append("、");
}
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
} else if (value.contains("/")) {
String[] range = value.split("/");
int start = Integer.parseInt(range[0]);
int step = Integer.parseInt(range[1]);
StringBuilder sb = new StringBuilder();
for (int i = start; i < list.size(); i += step) {
sb.append(list.get(i).getLabel()).append("、");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
} else if (value.contains("-")) {
String[] range = value.split("-");
int start = Integer.parseInt(range[0]);
int end = Integer.parseInt(range[1]);
StringBuilder sb = new StringBuilder();
for (int i = start; i <= end; i++) {
sb.append(list.get(i).getLabel()).append("、");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
} else if ("L".equals(value)) {
return list.get(list.size() - 1).getLabel();
} else {
int index = Integer.parseInt(value);
if (index < list.size()) {
return list.get(index).getLabel();
}
}
return "";
}
private static String expandDayAsSpecifiedValues(String value, List<ValueLabelPair> list) {
if ("*".equals(value)) {
return "";
} else if (value.contains(",")) {
String[] values = value.split(",");
StringBuilder sb = new StringBuilder();
for (String v : values) {
if (v.contains("/")) {
String[] range = v.split("/");
int start = Integer.parseInt(range[0]);
int step = Integer.parseInt(range[1]);
for (int i = start; i <= list.size(); i += step) {
sb.append(list.get(i - 1).getLabel()).append("、");
}
} else if (v.contains("-")) {
String[] range = v.split("-");
int start = Integer.parseInt(range[0]);
int end = Integer.parseInt(range[1]);
for (int i = start; i <= end; i++) {
sb.append(list.get(i - 1).getLabel()).append("、");
}
} else if ("L".equals(v)) {
sb.append(list.get(list.size() - 1).getLabel()).append("、");
} else {
int index = Integer.parseInt(v);
sb.append(list.get(index - 1).getLabel()).append("、");
}
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
} else if (value.contains("/")) {
String[] range = value.split("/");
int start = Integer.parseInt(range[0]);
int step = Integer.parseInt(range[1]);
StringBuilder sb = new StringBuilder();
for (int i = start; i <= list.size(); i += step) {
sb.append(list.get(i - 1).getLabel()).append("、");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
} else if (value.contains("-")) {
String[] range = value.split("-");
int start = Integer.parseInt(range[0]);
int end = Integer.parseInt(range[1]);
StringBuilder sb = new StringBuilder();
for (int i = start; i <= end; i++) {
sb.append(list.get(i - 1).getLabel()).append("、");
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
} else if ("L".equals(value)) {
return list.get(list.size() - 1).getLabel();
} else {
int index = Integer.parseInt(value);
if (index <= list.size()) {
return list.get(index - 1).getLabel();
}
}
return "";
}
public static List<ValueLabelPair> generateValueLabelPairs(int start, int end) {
List<ValueLabelPair> valueLabelPairs = new ArrayList<>();
for (int i = start; i <= end; i++) {
String value = String.valueOf(i);
String label = String.format("%02d", i);
valueLabelPairs.add(new ValueLabelPair(value, label));
}
return valueLabelPairs;
}
public static List<ValueLabelPair> generateDateValueLabelPairs(int start, int end) {
List<ValueLabelPair> valueLabelPairs = new ArrayList<>();
for (int i = start; i <= end; i++) {
String value = String.valueOf(i);
String label = String.valueOf(i);
valueLabelPairs.add(new ValueLabelPair(value, label));
}
valueLabelPairs.add(new ValueLabelPair("L", "最后一日"));
return valueLabelPairs;
}
static class ValueLabelPair {
private final String value;
private final String label;
public ValueLabelPair(String value, String label) {
this.value = value;
this.label = label;
}
public String getValue() {
return value;
}
public String getLabel() {
return label;
}
}
}