点击查看代码
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CronUtil {
private static Map<String, String> monthMap = new HashMap<>();
private static Map<String, String> weekMap = new HashMap<>();
private static Map<Character, String> weekDayMap = new HashMap<>();
private static Map<Integer, String> nameMap = new HashMap<>();
private static Map<Integer, String> eMap = new HashMap<>();
private static final String COMMA = ",";
private static final String STAR = "*";
private static final String LAST = "L";
private static final String UNDEFINED = "?";
private static final String ZERO = "0";
private static final Integer YEAR = 6;
private static final Integer WEEK = 5;
private static final Integer MONTH = 4;
private static final Integer DAY = 3;
private static final Integer HOUR = 2;
private static final Integer MINUTE = 1;
private static final Integer SECOND = 0;
private static final String E_YEAR = "每年";
private static final String E_WEEK = "每周";
private static final String E_MONTH = "每月";
private static final String E_DAY = "每日";
private static final String E_HOUR = "每时";
private static final String E_MINUTE = "每分";
private static final String E_SECOND = "每秒";
static {
monthMap.put("JAN", "1月");
monthMap.put("FEB", "2月");
monthMap.put("MAR", "3月");
monthMap.put("APR", "4月");
monthMap.put("MAY", "5月");
monthMap.put("JUN", "6月");
monthMap.put("JUL", "7月");
monthMap.put("AUG", "8月");
monthMap.put("SEP", "9月");
monthMap.put("OCT", "10月");
monthMap.put("NOV", "11月");
monthMap.put("DEC", "12月");
weekMap.put("MON", "星期一");
weekMap.put("TUE", "星期二");
weekMap.put("WED", "星期三");
weekMap.put("THU", "星期四");
weekMap.put("FRI", "星期五");
weekMap.put("SAT", "星期六");
weekMap.put("SUN", "星期日");
weekDayMap.put('1', "星期日");
weekDayMap.put('2', "星期一");
weekDayMap.put('3', "星期二");
weekDayMap.put('4', "星期三");
weekDayMap.put('5', "星期四");
weekDayMap.put('6', "星期五");
weekDayMap.put('7', "星期六");
nameMap.put(SECOND, "秒");
nameMap.put(MINUTE, "分");
nameMap.put(HOUR, "时");
nameMap.put(DAY, "日");
nameMap.put(MONTH, "月");
nameMap.put(WEEK, "星期");
nameMap.put(YEAR, "年");
eMap.put(SECOND, E_SECOND);
eMap.put(MINUTE, E_MINUTE);
eMap.put(HOUR, E_HOUR);
eMap.put(DAY, E_DAY);
eMap.put(MONTH, E_MONTH);
eMap.put(WEEK, E_WEEK);
eMap.put(YEAR, E_YEAR);
}
public static String translateCron(String cron) {
String[] fields = cron.split(" ");
if (fields.length < 6 || fields.length > 7) {
throw new UnsupportedOperationException("无效的cron表达式");
}
StringBuilder result = new StringBuilder();
if (fields.length == 7) {
parseYear(fields[6], result);
}
parseMonth(fields[4], result);
parseWeek(fields[5], result);
boolean dayFlag = (STAR.equals(fields[5]) || UNDEFINED.equals(fields[5])) && (STAR.equals(fields[4]) || UNDEFINED.equals(fields[4]));
parseDay(fields[3], dayFlag, result);
boolean hourFlag = (STAR.equals(fields[3]) || UNDEFINED.equals(fields[3])) && (STAR.equals(fields[4]) || UNDEFINED.equals(fields[4]));
parseHour(fields[2], hourFlag, result);
boolean minuteFlag = STAR.equals(fields[2]) || UNDEFINED.equals(fields[2]);
parseMinute(fields[1], minuteFlag, result);
boolean secondFlag = STAR.equals(fields[1]) || UNDEFINED.equals(fields[1]);
parseSecond(fields[0], secondFlag, result);
result.append("执行一次");
return result.toString();
}
public static void parseYear(String field, StringBuilder result) {
if (!UNDEFINED.equals(field) && !STAR.equals(field)) {
result.append(field).append(nameMap.get(YEAR));
}
}
public static void parseMonth(String field, StringBuilder result) {
if (!UNDEFINED.equals(field) && !STAR.equals(field)) {
if (result.length() == 0) {
result.append(E_YEAR);
}
if (monthMap.containsKey(field)) {
result.append(monthMap.get(field));
} else if (field.contains("-")) {
String[] split = field.split("-");
if (monthMap.containsKey(split[0])) {
result.append(monthMap.get(split[0])).append("至").append(monthMap.get(split[1]));
} else {
result.append(split[0] + nameMap.get(MONTH)).append("至").append(split[1] + nameMap.get(MONTH));
}
} else {
result.append(field).append(nameMap.get(MONTH));
}
}
}
public static void parseWeek(String field, StringBuilder result) {
if (!UNDEFINED.equals(field) && !STAR.equals(field)) {
if (result.length() == 0 && !field.contains(LAST) && field.contains("#")) {
result.append(E_WEEK);
}
if (weekMap.containsKey(field)) {
result.append(weekMap.get(field));
} else if (field.contains("-")) {
String[] split = field.split("-");
if (weekMap.containsKey(split[0])) {
result.append(weekMap.get(split[0])).append("至").append(weekMap.get(split[1]));
} else {
result.append(weekDayMap.get(split[0])).append("至").append(weekDayMap.get(split[1]));
}
} else if (field.contains(LAST)) {
result.append(E_MONTH);
result.append("最后一个").append(weekDayMap.get(field.charAt(0)));
} else if (field.contains("#")) {
String[] split = field.split("#");
result.append(E_MONTH);
result.append("第" + split[1] + "个").append(weekDayMap.get(field.charAt(0)));
} else {
result.append(weekDayMap.get(field));
}
}
}
public static void parseDay(String field, boolean flag, StringBuilder result) {
if (!UNDEFINED.equals(field) && !STAR.equals(field)) {
if (result.length() == 0 || flag) {
result.append(E_MONTH);
}
if (field.equals(LAST)) {
result.append("最后一天");
} else if (field.contains("-")) {
String[] split = field.split("-");
result.append(split[0] + nameMap.get(DAY)).append("至").append(split[1] + nameMap.get(DAY));
} else if (field.contains(COMMA)) {
String[] split = field.split(COMMA);
String collect = Arrays.stream(split).map(o -> o + nameMap.get(DAY)).collect(Collectors.joining("、"));
result.append(collect);
} else {
result.append(field).append(nameMap.get(DAY));
}
}
}
private static void parseHour(String field, boolean flag, StringBuilder result) {
if (!UNDEFINED.equals(field) && !STAR.equals(field)) {
if (result.length() == 0 || flag) {
result.append(E_DAY);
}
if (field.contains("-")) {
String[] split = field.split("-");
result.append("从").append(hour2Cn(Integer.valueOf(split[0]))).append("至").append(hour2Cn(Integer.valueOf(split[1])));
} else if (field.contains("/")) {
String[] split = field.split("/");
result.append("从").append(hour2Cn(Integer.valueOf(split[0]))).append("每").append(split[1]).append(nameMap.get(HOUR));
} else if (field.contains(COMMA)) {
String[] split = field.split(COMMA);
String collect = Arrays.stream(split).map(o -> hour2Cn(Integer.valueOf(o))).collect(Collectors.joining("、"));
result.append(collect);
} else {
result.append(hour2Cn(Integer.valueOf(field)));
}
}
}
private static void parseMinute(String field, boolean flag, StringBuilder result) {
if (!UNDEFINED.equals(field) && !STAR.equals(field)) {
if (result.length() == 0 || flag) {
result.append(E_HOUR);
}
if (field.contains("-")) {
String[] split = field.split("-");
result.append("从").append(split[0]).append(nameMap.get(MINUTE)).append("至").append(split[1]).append(nameMap.get(MINUTE));
} else if (field.contains("/")) {
String[] split = field.split("/");
if (!ZERO.equals(split[0])) {
result.append("从").append(split[0]).append(nameMap.get(MINUTE)).append("开始");
}
result.append(",每").append(split[1]).append(nameMap.get(MINUTE));
} else if (field.contains(COMMA)) {
String[] split = field.split(COMMA);
String collect = Arrays.stream(split).map(o -> o + nameMap.get(MINUTE)).collect(Collectors.joining("、"));
result.append(collect);
} else if (ZERO.equals(field)) {
} else {
result.append(field).append(nameMap.get(MINUTE));
}
}
}
private static void parseSecond(String field, boolean flag, StringBuilder result) {
if (!UNDEFINED.equals(field) && !STAR.equals(field)) {
if (result.length() == 0 || flag) {
result.append(E_MINUTE);
}
if (field.contains("-")) {
String[] split = field.split("-");
result.append("从").append(split[0]).append(nameMap.get(SECOND)).append("至").append(split[1]).append(nameMap.get(SECOND));
} else if (field.contains("/")) {
String[] split = field.split("/");
if (!ZERO.equals(split[0])) {
result.append("从").append(split[0]).append(nameMap.get(SECOND)).append("开始");
}
result.append(",每").append(split[1]).append(nameMap.get(SECOND));
} else if (field.contains(COMMA)) {
String[] split = field.split(COMMA);
String collect = Arrays.stream(split).map(o -> o + nameMap.get(SECOND)).collect(Collectors.joining("、"));
result.append(collect);
} else if (ZERO.equals(field)) {
//不做任何处理
} else {
result.append(field).append(nameMap.get(SECOND));
}
}
}
private static String hour2Cn(Integer value) {
if (null == value) {
return "";
}
if (value < 24 && value >= 18) {
return "晚上" + (value - 12) + "点";
} else if (value > 12 && value < 18) {
return "下午" + (value - 12) + "点";
} else if (value == 12) {
return "中午12点";
} else if (value < 12 && value >= 6) {
return "上午" + value + "点";
} else if (value < 6 && value >= 0) {
return "凌晨" + value + "点";
}
return String.valueOf(value);
}
public static void main(String[] args) {
List<String> crons = Arrays.asList(
"0 0 10,14,16 * * ?",
"0 0/30 9-17 * * ?",
"0 0 12 ? * WED",
"0 0 12 * * ?",
"0 15 10 ? * *",
"0 15 10 * * ? ",
"0 15 10 * * ? 2005",
"0 * 14 * * ?",
"0 0/5 14 * * ?",
"0 0/5 14,18 * * ?",
"0 0-5 14 * * ?",
"0 10,44 14 ? 3 WED"
);
crons.forEach(cron -> {
String s = translateCron(cron);
System.out.println(s);
});
}
}