Spring cron表达式翻译成中文
先上运行结果:
0 0 0/1 5,7,23 * ?
每月5,7,23号的0时开始每隔1小时的0分0秒
0 0 8 ? 4-7 MON,WED,FRI
4月到7月每周1,周3,周5,8时0分0秒
0 14 18 ? * MON,TUE,SAT,SUN
每周一、二、六、日的18点
0 0 8 ? * MON-FRI
每周1到周5的8时
改写的别人代码 原文链接:
链接: https://www.cnblogs.com/pejsidney/p/12809673.html.
比原文稍微复杂点考虑的多些
package com.geadmin.modules.tools.util;
import cn.geboot.core.util.StringUtil;
import org.quartz.CronExpression;
import java.text.ParseException;
public class CronUtil {
public static String translateToChinese(String cronExp) {
if (cronExp == null || cronExp.length() < 1) {
return "cron表达式为空";
}
CronExpression exp = null;
// 初始化cron表达式解析器
try {
exp = new CronExpression(cronExp);
} catch (ParseException e) {
return "corn表达式不正确";
}
String[] tmpCorns = cronExp.split(" ");
StringBuffer sBuffer = new StringBuffer();
StringBuffer sBuffer2 = new StringBuffer();
if (tmpCorns.length == 6) {
//解析月
if (tmpCorns[4].equals("*")) {
sBuffer.append("");
}
else if (tmpCorns[4].indexOf("-")!=-1){
String[] split = tmpCorns[4].split("-");
sBuffer.append(split[0]+"月到"+split[1]+"月");
}
else {
sBuffer.append(tmpCorns[4]).append("月");
}
//解析周
if (!tmpCorns[5].equals("*") && !tmpCorns[5].equals("?")) {
if (tmpCorns[5].indexOf("-")!=-1){
String[] split = tmpCorns[5].split("-");
sBuffer.append("每周"+getWeek(split[0])+"到周"+getWeek(split[1]));
}else {
String[] split = tmpCorns[5].split(",");
sBuffer.append("每周");
for (String tmp : split) {
sBuffer.append(getWeek(tmp)+"、");
}
}
}
//解析日
if (!tmpCorns[3].equals("?")) {
if (tmpCorns[3].equals("*")){
sBuffer.append("每天");
}else if (tmpCorns[3].indexOf("-")!=-1){
String[] split = tmpCorns[3].split("-");
sBuffer.append(split[0]+"号到"+split[1]+"号,");
}else if (tmpCorns[3].indexOf("/")!=-1){
String[] split = tmpCorns[3].split("/");
sBuffer.append(split[0]+"号开始每"+split[1]+"天的,");
}else if(tmpCorns[3].indexOf("W")!=-1){
sBuffer.append(StringUtil.removeSuffix(tmpCorns[3], "W")+"号最近的那个工作日");
}else if (tmpCorns[3].equals("L")){
sBuffer.append("每月最后一天");
}else{
sBuffer.append(tmpCorns[3]+"号");
}
}
String s = StringUtil.removeSuffix(sBuffer, "、")+"的";
sBuffer2 = new StringBuffer(s);
//解析时
if (tmpCorns[2].equals("*")) {
sBuffer2.append("每时");
}else if (tmpCorns[2].indexOf("-")!=-1){
String[] split = tmpCorns[3].split("-");
sBuffer2.append(split[0]+"时到"+split[1]+"时,");
}else if (tmpCorns[2].indexOf("/")!=-1){
String[] split = tmpCorns[2].split("/");
sBuffer2.append(split[0]+"时开始每隔"+split[1]+"小时");
}else{
sBuffer2.append(tmpCorns[2]+"时");
}
/* //解析分
if (!tmpCorns[1].equals("*")) {
sBuffer2.append(tmpCorns[1]).append("分");
} else {
sBuffer2.append("每分");
}
//解析秒
if (!tmpCorns[0].equals("*")) {
sBuffer2.append(tmpCorns[0]).append("秒");
} else {
sBuffer2.append("每秒");
}*/
}
return sBuffer2.toString();
}
public static String getWeek(String tmp){
switch (tmp) {
case "MON":
return ("1");
case "TUE":
return("2");
case "WED":
return("3");
case "THU":
return("4");
case "FRI":
return("5");
case "SAT":
return("6");
case "SUN":
return("7");
default:
return(tmp);
}
}
/* //测试方法
public static void main(String[] args) {
//String CRON_EXPRESSION = "0 0 8 ? 4-7 MON,WED,FRI";
String CRON_EXPRESSION = "0 0 0/1 5,7,23 * ?";
System.out.println(CRON_EXPRESSION);
System.out.println(CronExpParser.translateToChinese(CRON_EXPRESSION));
}*/
}
就到这里了
代码不是怎么好 随便写的不喜勿喷 懒癌晚期没得治
喜欢看动漫的强烈推荐 少女终末旅行 和 来自深渊 超好看的!