提交修改
This commit is contained in:
parent
67f065057e
commit
2e190a8edd
@ -208,7 +208,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
/**
|
||||
* 日期型字符串转化为日期 格式
|
||||
*/
|
||||
public static Date myParseDate(Object str) {
|
||||
public static Date parseDate(Object str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ public class SqlDateParse {
|
||||
Date date = DateUtils.addDays(new Date(), days);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(formate);
|
||||
String dateStr = sdf.format(date);
|
||||
Date date1 = DateUtils.myParseDate(dateStr);
|
||||
Date date1 = DateUtils.parseDate(dateStr);
|
||||
return date1.getTime();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@ -2,6 +2,8 @@ package com.heyu.api.schedule;
|
||||
|
||||
import com.heyu.api.data.utils.DateUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.quartz.CronExpression;
|
||||
import org.quartz.TriggerUtils;
|
||||
import org.quartz.impl.triggers.CronTriggerImpl;
|
||||
import org.springframework.scheduling.support.CronSequenceGenerator;
|
||||
@ -46,7 +48,7 @@ public class CronTriggerUtils {
|
||||
*/
|
||||
public static Date getCurrentExecTime(String cronExpression) {
|
||||
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
|
||||
Date lastMinute= DateUtils.getBeforeMinute(new Date(),1);
|
||||
Date lastMinute = DateUtils.getBeforeMinute(new Date(), 1);
|
||||
Date nextTriggerTime = cronSequenceGenerator.next(lastMinute);//lastMinute 我这里是上一分钟的date类型对象
|
||||
return nextTriggerTime;
|
||||
}
|
||||
@ -74,25 +76,161 @@ public class CronTriggerUtils {
|
||||
}
|
||||
|
||||
|
||||
public static String cronCurrentDate(Date currentDate, String cronExpression) {
|
||||
try {
|
||||
TimeLevelEnums enums = TimeLevelEnums.getTimeLevel(cronExpression);
|
||||
System.out.println(enums);
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
if (TimeLevelEnums.years.equals(enums)) {
|
||||
Date date = DateUtils.formateDate(currentDate, "yyyy-00-00 00:00:00");
|
||||
return dateFormat.format(date);
|
||||
} else if (TimeLevelEnums.months.equals(enums)) {
|
||||
Date date = DateUtils.formateDate(currentDate, "yyyy-MM-00 00:00:00");
|
||||
return dateFormat.format(date);
|
||||
} else if (TimeLevelEnums.days.equals(enums)) {
|
||||
Date date = DateUtils.formateDate(currentDate, "yyyy-MM-dd 00:00:00");
|
||||
return dateFormat.format(date);
|
||||
} else if (TimeLevelEnums.hours.equals(enums)) {
|
||||
Date date = DateUtils.formateDate(currentDate, "yyyy-MM-dd HH:00:00");
|
||||
return dateFormat.format(date);
|
||||
} else if (TimeLevelEnums.minutes.equals(enums)) {
|
||||
Date date = DateUtils.formateDate(currentDate, "yyyy-MM-dd HH:mm:00");
|
||||
return dateFormat.format(date);
|
||||
} else if (TimeLevelEnums.seconds.equals(enums)) {
|
||||
Date date = DateUtils.formateDate(currentDate, "yyyy-MM-dd HH:mm:ss");
|
||||
return dateFormat.format(date);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean isRun(Date time, String cronExpression) {
|
||||
try {
|
||||
Date dateStr = DateUtils.formateDate(time, "yyyy-MM-dd HH:mm:00");
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
Date date = DateUtils.parseDate(dateFormat.format(dateStr));
|
||||
CronExpression cronMonth = new CronExpression(cronExpression);
|
||||
if (cronMonth.isSatisfiedBy(date)) {
|
||||
return true;
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
|
||||
String cronExpression = "0 0 12 * * ?";
|
||||
|
||||
// String currentDate = cronCurrentDate(new Date(), cronExpression);
|
||||
|
||||
// System.out.println(currentDate);
|
||||
|
||||
|
||||
Date date = DateUtils.parseDate("2025-10-19 12:01:00");
|
||||
|
||||
System.out.println(isRun(date, cronExpression));
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* https://help.aliyun.com/document_detail/133509.html
|
||||
*
|
||||
*/
|
||||
// 示例
|
||||
// */5 * * * * ?:每隔 5 秒执行一次
|
||||
//
|
||||
// 0 */1 * * * ?:每隔 1 分钟执行一次
|
||||
//
|
||||
// 0 0 2 1 * ?:每月 1 日的凌晨 2 点执行一次
|
||||
//
|
||||
// 0 15 10 ? * MON-FRI:周一到周五每天上午 10:15 执行作业
|
||||
//
|
||||
// 0 15 10 ? 6L 2002-2006:2002 年至 2006 年的每个月的最后一个星期五上午 10:15 执行作业
|
||||
//
|
||||
// 0 0 23 * * ?:每天 23 点执行一次
|
||||
//
|
||||
// 0 0 1 * * ?:每天凌晨 1 点执行一次
|
||||
//
|
||||
// 0 0 1 1 * ?:每月 1 日凌晨 1 点执行一次
|
||||
//
|
||||
// 0 0 23 L * ?:每月最后一天 23 点执行一次
|
||||
//
|
||||
// 0 0 1 ? * L:每周星期天凌晨 1 点执行一次
|
||||
//
|
||||
// 0 26,29,33 * * * ?:在 26 分、29 分、33 分执行一次
|
||||
//
|
||||
// 0 0 0,13,18,21 * * ?:每天的 0 点、13 点、18 点、21 点都执行一次
|
||||
//
|
||||
// 0 0 10,14,16 * * ?:每天上午 10 点,下午 2 点,4 点执行一次
|
||||
//
|
||||
// 0 0/30 9-17 * * ?:朝九晚五工作时间内每半小时执行一次
|
||||
//
|
||||
// 0 0 12 ? * WED:每个星期三中午 12 点执行一次
|
||||
//
|
||||
// 0 0 12 * * ?:每天中午 12 点触发
|
||||
//
|
||||
// 0 15 10 ? * *:每天上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 * * ?:每天上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 * * ? *:每天上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 * * ? 2005:2005 年的每天上午 10:15 触发
|
||||
//
|
||||
// 0 * 14 * * ?:每天下午 2 点到 2:59 期间的每 1 分钟触发
|
||||
//
|
||||
// 0 0/5 14 * * ?:每天下午 2 点到 2:55 期间的每 5 分钟触发
|
||||
//
|
||||
// 0 0/5 14,18 * * ?:每天下午 2 点到 2:55 期间和下午 6 点到 6:55 期间的每 5 分钟触发
|
||||
//
|
||||
// 0 0-5 14 * * ?:每天下午 2 点到 2:05 期间的每 1 分钟触发
|
||||
//
|
||||
// 0 10,44 14 ? 3 WED:每年三月的星期三的下午 2:10 和 2:44 触发
|
||||
//
|
||||
// 0 15 10 ? * MON-FRI:周一至周五的上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 15 * ?:每月 15 日上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 L * ?:每月最后一日的上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 ? * 6L:每月的最后一个星期五上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 ? * 6L 2002-2005:2002 年至 2005 年的每月的最后一个星期五上午 10:15 触发
|
||||
//
|
||||
// 0 15 10 ? * 6#3:每月的第三个星期五上午 10:15 触发
|
||||
public static void main(String[] args) throws Exception {
|
||||
String cronExpression = "0 0 0 1 * ?";
|
||||
String cronExpression = "0 */1 * * * ?";
|
||||
CronExpression cronMonth = new CronExpression(cronExpression);
|
||||
Date date = DateUtils.formateDate(new Date(), "yyyy-MM-dd HH:mm:00");
|
||||
|
||||
System.out.println("xxxxxxxxxx" + date);
|
||||
|
||||
|
||||
Date nextTunTime = CronTriggerUtils.getCurrentExecTime(cronExpression);
|
||||
System.out.println(nextTunTime);
|
||||
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
System.out.println(TimeLevelEnums.getTimeLevel(cronExpression));
|
||||
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
log.info("执行时间:" + df.format(nextTunTime) + ",currentDate :{}", df.format(new Date()));
|
||||
System.out.println(df.format(nextTunTime));
|
||||
Date nextTriggerTime = cronSequenceGenerator.next(nextTunTime);//lastMinute 我这里是上一分钟的date类型对象
|
||||
nextTunTime = nextTriggerTime;
|
||||
}
|
||||
|
||||
|
||||
if (cronMonth.isSatisfiedBy(date)) {
|
||||
System.out.println("xxxxxxxx");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
package com.heyu.api.schedule;
|
||||
|
||||
import com.heyu.api.data.utils.DateUtils;
|
||||
import org.springframework.scheduling.support.CronSequenceGenerator;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public enum TimeLevelEnums {
|
||||
years("年"),
|
||||
@ -29,25 +33,53 @@ public enum TimeLevelEnums {
|
||||
}
|
||||
|
||||
|
||||
public static TimeLevelEnums getTimeLevel(String date) {
|
||||
|
||||
if(date == null) {
|
||||
return null;
|
||||
public static TimeLevelEnums getTimeLevel(String cronExpression) {
|
||||
try {
|
||||
Date nextTunTime = CronTriggerUtils.getCurrentExecTime(cronExpression);
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
|
||||
List<String> nextTimes = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
nextTimes.add( df.format(nextTunTime));
|
||||
Date nextTriggerTime = cronSequenceGenerator.next(nextTunTime);//lastMinute 我这里是上一分钟的date类型对象
|
||||
nextTunTime = nextTriggerTime;
|
||||
}
|
||||
|
||||
String aStr = nextTimes.get(0);
|
||||
String bStr = nextTimes.get(1);
|
||||
char arrayA []= aStr.toCharArray();
|
||||
char arrayB[] = bStr.toCharArray();
|
||||
int eq = 0;
|
||||
for (int i = 0; i < arrayA.length; i++) {
|
||||
char a = arrayA[i];
|
||||
char b = arrayB[i];
|
||||
if (a == b) {
|
||||
eq++;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
//2025-10-20 18:27:00
|
||||
if(eq < 4){
|
||||
return TimeLevelEnums.years;
|
||||
}else if(eq < 7){
|
||||
return TimeLevelEnums.months;
|
||||
}else if(eq < 10){
|
||||
return TimeLevelEnums.days;
|
||||
}else if(eq < 13){
|
||||
return TimeLevelEnums.hours;
|
||||
}else if(eq < 16){
|
||||
return TimeLevelEnums.minutes;
|
||||
}else if (eq>=17){
|
||||
return TimeLevelEnums.seconds;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if(date.endsWith(" 00:00:00")){
|
||||
return TimeLevelEnums.months;
|
||||
}else if(date.endsWith(" 00:00:00")){
|
||||
|
||||
|
||||
|
||||
return TimeLevelEnums.months;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static String getDfCurrentDate(TimeLevelEnums enums){
|
||||
if(TimeLevelEnums.years.equals(enums)){
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user