|
|
|
@ -3,8 +3,13 @@ package com.example.zxweb.utils;
|
|
|
|
|
import com.example.zxweb.common.constant.enums.WeekEnum;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Description 简单型Cron
|
|
|
|
@ -13,7 +18,8 @@ import java.text.DecimalFormat;
|
|
|
|
|
*/
|
|
|
|
|
public class CronLowUtil {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String timeType="everyDay";
|
|
|
|
|
System.out.println(cronToText("* * * * * 1,2 "));
|
|
|
|
|
/*String timeType="everyDay";
|
|
|
|
|
String exeTime="08:30";
|
|
|
|
|
String weeklyDay="1,2,3";//周一,周二,周三
|
|
|
|
|
String day="1,31";
|
|
|
|
@ -21,55 +27,165 @@ public class CronLowUtil {
|
|
|
|
|
String endDate="2023-09-30";//结束时间
|
|
|
|
|
System.out.println(getCron(timeType,exeTime,weeklyDay,day,startDate,endDate));
|
|
|
|
|
//cron转 显示文本
|
|
|
|
|
System.out.println(cronToText(getCron(timeType,exeTime,weeklyDay,day,startDate,endDate)));
|
|
|
|
|
System.out.println(cronToText(getCron(timeType,exeTime,weeklyDay,day,startDate,endDate)));*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param timeType 时间类型(everyDay:每天,weekly:每周,day:每天,date:日期段)
|
|
|
|
|
* @param exeTime 执行时间(HH:mm 例:08:30)
|
|
|
|
|
* @param timeType 时间类型(everyDay:每天,weekly:每周,day:每天,date:日期段)
|
|
|
|
|
* @param exeTime 执行时间(HH:mm 例:08:30)
|
|
|
|
|
* @param weeklyDay 1,2,3:周一,周二,周三
|
|
|
|
|
* @param day 每月的第几天,1,31 每月1日,31日
|
|
|
|
|
* @param day 每月的第几天,1,31 每月1日,31日
|
|
|
|
|
* @param startDate 范围型,开始日期 2023-09-10
|
|
|
|
|
* @param endDate 范围型,截止日期 2023-09-30
|
|
|
|
|
* @param endDate 范围型,截止日期 2023-09-30
|
|
|
|
|
* @return cron
|
|
|
|
|
*/
|
|
|
|
|
public static String getCron(String timeType,String exeTime,String weeklyDay,String day,String startDate,String endDate) {
|
|
|
|
|
public static String getCron(String timeType, String exeTime, String weeklyDay, String day, String startDate, String endDate) {
|
|
|
|
|
String[] time = exeTime.split(":");
|
|
|
|
|
Integer hour=Integer.parseInt(time[0]);//小时
|
|
|
|
|
Integer minute=Integer.parseInt(time[1]);//分钟
|
|
|
|
|
Integer seconds=Integer.parseInt(time[2]);//秒
|
|
|
|
|
String cron=null;
|
|
|
|
|
Integer hour = Integer.parseInt(time[0]);//小时
|
|
|
|
|
Integer minute = Integer.parseInt(time[1]);//分钟
|
|
|
|
|
Integer seconds = 0;//Integer.parseInt(time[2]);//秒
|
|
|
|
|
String cron = null;
|
|
|
|
|
//每天
|
|
|
|
|
if ("everyDay".equals(timeType)) {
|
|
|
|
|
cron = String.format("%s %s %s * * ? *",seconds,minute,hour);
|
|
|
|
|
cron = String.format("%s %s %s * * ? *", seconds, minute, hour);
|
|
|
|
|
} else if ("weekly".equals(timeType)) {
|
|
|
|
|
//每周
|
|
|
|
|
cron = String.format("0 %s %s ? * %s",minute,hour,weeklyDay);
|
|
|
|
|
} else if("day".equals(timeType)){
|
|
|
|
|
cron = String.format("0 %s %s ? * %s", minute, hour, weeklyDay);
|
|
|
|
|
} else if ("day".equals(timeType)) {
|
|
|
|
|
//单日
|
|
|
|
|
cron = String.format("0 %s %s %s * ?",minute,hour,day);
|
|
|
|
|
} else if ("date".equals(timeType)){
|
|
|
|
|
cron = String.format("0 %s %s ? * ?", minute, hour/*, day*/);
|
|
|
|
|
} else if ("date".equals(timeType)) {
|
|
|
|
|
//日期段
|
|
|
|
|
System.out.println("在"+startDate+"到"+endDate);
|
|
|
|
|
cron = String.format("0 %s %s * * ? *",minute,hour);
|
|
|
|
|
System.out.println("在" + startDate + "到" + endDate);
|
|
|
|
|
cron = String.format("0 %s %s * * ? *", minute, hour);
|
|
|
|
|
}
|
|
|
|
|
return cron;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将Cron 解释成文本
|
|
|
|
|
*
|
|
|
|
|
* @param cron cron
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String cronToText(String cron){
|
|
|
|
|
public static String cronToText(String cron) {
|
|
|
|
|
if (StringUtils.isBlank(cron)) {
|
|
|
|
|
return "未知";
|
|
|
|
|
}
|
|
|
|
|
StringBuilder sb=new StringBuilder();
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
String[] cronSplit = cron.split(" ");
|
|
|
|
|
|
|
|
|
|
for (int i = cronSplit.length-1; i >= 0;i--) {
|
|
|
|
|
List<String> years = null;//年
|
|
|
|
|
List<String> weeklys = null;//周
|
|
|
|
|
List<String> months = null;//月
|
|
|
|
|
List<String> days = null;//日
|
|
|
|
|
List<String> hours = null;//小时
|
|
|
|
|
List<String> minutes = null;//分钟
|
|
|
|
|
List<String> seconds = null;//秒
|
|
|
|
|
|
|
|
|
|
List<String> nots = Arrays.asList("?", "*");
|
|
|
|
|
if (cronSplit.length >= 7 && !nots.contains(cronSplit[6])) {
|
|
|
|
|
years = Arrays.asList(cronSplit[6].split(","));
|
|
|
|
|
}
|
|
|
|
|
if (cronSplit.length >= 6 && !nots.contains(cronSplit[5])) {
|
|
|
|
|
weeklys = Arrays.asList(cronSplit[5].split(","));
|
|
|
|
|
}
|
|
|
|
|
if (cronSplit.length >= 5 && !nots.contains(cronSplit[4])) {
|
|
|
|
|
months = Arrays.asList(cronSplit[4].split(","));
|
|
|
|
|
}
|
|
|
|
|
if (cronSplit.length >= 4 && !nots.contains(cronSplit[3])) {
|
|
|
|
|
days = Arrays.asList(cronSplit[3].split(","));
|
|
|
|
|
}
|
|
|
|
|
if (cronSplit.length >= 3 && !nots.contains(cronSplit[2])) {
|
|
|
|
|
hours = Arrays.asList(cronSplit[2].split(","));
|
|
|
|
|
}
|
|
|
|
|
if (cronSplit.length >= 2 && !nots.contains(cronSplit[1])) {
|
|
|
|
|
minutes = Arrays.asList(cronSplit[1].split(","));
|
|
|
|
|
}
|
|
|
|
|
if (cronSplit.length >= 1 && !nots.contains(cronSplit[0])) {
|
|
|
|
|
seconds = Arrays.asList(cronSplit[0].split(","));
|
|
|
|
|
}
|
|
|
|
|
//年
|
|
|
|
|
if (!CollectionUtils.isEmpty(years)) {
|
|
|
|
|
StringBuilder sb2 = new StringBuilder();
|
|
|
|
|
for (String year : years) {
|
|
|
|
|
sb2.append(year).append("年,");
|
|
|
|
|
}
|
|
|
|
|
if (sb2.length() > 0) {
|
|
|
|
|
sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//周
|
|
|
|
|
if (!CollectionUtils.isEmpty(weeklys) ) {
|
|
|
|
|
StringBuilder sb2 = new StringBuilder();
|
|
|
|
|
for (String week : weeklys) {
|
|
|
|
|
sb2.append("周").append(NumberToCnUtil.toChineseLower(Integer.parseInt(week))).append(",");
|
|
|
|
|
}
|
|
|
|
|
if (sb2.length() > 0) {
|
|
|
|
|
sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//月
|
|
|
|
|
if (!CollectionUtils.isEmpty(months)) {
|
|
|
|
|
StringBuilder sb2 = new StringBuilder();
|
|
|
|
|
for (String month : months) {
|
|
|
|
|
sb2.append(month).append("月").append(",");
|
|
|
|
|
}
|
|
|
|
|
if (sb2.length() > 0) {
|
|
|
|
|
sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//日
|
|
|
|
|
if (!CollectionUtils.isEmpty(days)) {
|
|
|
|
|
StringBuilder sb2 = new StringBuilder();
|
|
|
|
|
for (String day : days) {
|
|
|
|
|
sb2.append(day).append("日").append(",");
|
|
|
|
|
}
|
|
|
|
|
if (sb2.length() > 0) {
|
|
|
|
|
sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<String> hhmmssList=new ArrayList<>();
|
|
|
|
|
//时
|
|
|
|
|
if (!CollectionUtils.isEmpty(hours)) {
|
|
|
|
|
for (String hour : hours) {
|
|
|
|
|
String hhmm=new DecimalFormat("00").format(Integer.parseInt(hour));
|
|
|
|
|
if (!CollectionUtils.isEmpty(minutes) && minutes.size()>0) {
|
|
|
|
|
for (String minute : minutes) {
|
|
|
|
|
hhmmssList.add(hhmm+":"+new DecimalFormat("00").format(Integer.parseInt(minute)));
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
hhmmssList.add(hhmm+":00");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append(String.join(",",hhmmssList));
|
|
|
|
|
//判断是不是指定日
|
|
|
|
|
//0 0 8 1 1 ? 2019-2019 2019-01-01 08:00
|
|
|
|
|
/*if (cronSplit.length>=7) {
|
|
|
|
|
String[] years = cronSplit[6].split(",");
|
|
|
|
|
if (years.length==1) {
|
|
|
|
|
//单年
|
|
|
|
|
String year = years[0];
|
|
|
|
|
String[] months = cronSplit[4].split(",");
|
|
|
|
|
if (months.length==1) {
|
|
|
|
|
//月
|
|
|
|
|
String month = months[0];
|
|
|
|
|
String[] days = cronSplit[3].split(",");
|
|
|
|
|
if (days.length==1) {
|
|
|
|
|
String day = days[0];
|
|
|
|
|
String hour = cronSplit[2];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
//多年
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
/*for (int i = cronSplit.length-1; i >= 0;i--) {
|
|
|
|
|
String c = cronSplit[i];
|
|
|
|
|
if (i==0) {
|
|
|
|
|
//秒
|
|
|
|
@ -78,12 +194,16 @@ public class CronLowUtil {
|
|
|
|
|
if (!"*".equals(c) && !"?".equals(c)) {
|
|
|
|
|
String[] split = c.split(",");
|
|
|
|
|
for (String s : split) {
|
|
|
|
|
sb.append(new DecimalFormat("00").format(Integer.parseInt(s))).append("");
|
|
|
|
|
sb.append(new DecimalFormat("00").format(Integer.parseInt(s)));
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
sb.append("00");
|
|
|
|
|
}
|
|
|
|
|
}else if(i==2){
|
|
|
|
|
//单天
|
|
|
|
|
if (sb.length()==0) {
|
|
|
|
|
sb.append("每天");
|
|
|
|
|
}
|
|
|
|
|
//小时
|
|
|
|
|
if (!"*".equals(c) && !"?".equals(c)) {
|
|
|
|
|
String[] split = c.split(",");
|
|
|
|
@ -97,7 +217,7 @@ public class CronLowUtil {
|
|
|
|
|
if (!"*".equals(c) && !"?".equals(c)) {
|
|
|
|
|
String[] split = c.split(",");
|
|
|
|
|
for (String s : split) {
|
|
|
|
|
daySb.append(s+"日").append(",");
|
|
|
|
|
daySb.append(s).append("日").append(",");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isNotBlank(daySb)) {
|
|
|
|
@ -110,28 +230,11 @@ public class CronLowUtil {
|
|
|
|
|
String[] split = c.split(",");
|
|
|
|
|
//sb.append("每周的 ");
|
|
|
|
|
for (String s : split) {
|
|
|
|
|
sb.append("周").append(NumberToCnUtil.toChineseLower(Integer.parseInt(s))).append(" ");
|
|
|
|
|
sb.append("周").append(NumberToCnUtil.toChineseLower(Integer.parseInt(s)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < cronSplit.length; i++) {
|
|
|
|
|
if (i==0) {
|
|
|
|
|
//秒
|
|
|
|
|
}else if(i==1){
|
|
|
|
|
//分钟
|
|
|
|
|
}else if(i==2){
|
|
|
|
|
//小时
|
|
|
|
|
}else if(i==3){
|
|
|
|
|
//天
|
|
|
|
|
}else if(i==4){//目前不需要做月
|
|
|
|
|
}else if(i==5){
|
|
|
|
|
//周
|
|
|
|
|
}else if(i==6){
|
|
|
|
|
//年 不需要
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println(sb);
|
|
|
|
|
}*/
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|