|
@@ -2,19 +2,149 @@ package com.example.opc_da.task;
|
|
|
|
|
|
import com.example.opc_common.util.DateUtil;
|
|
|
import com.example.opc_da.util.CronUtil;
|
|
|
+import org.quartz.CronExpression;
|
|
|
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
|
|
|
public class TestUtil {
|
|
|
- public static void main(String[] args) {
|
|
|
-
|
|
|
- String cron = "0 0/5 * * * ?";
|
|
|
- Integer type = 2;
|
|
|
- String newCron = CronUtil.genNextCron(cron, type);
|
|
|
- System.out.println(newCron);
|
|
|
- Date timeAfter = CronUtil.getTimeAfter(newCron, new Date());
|
|
|
- String itemId = DateUtil.dateChangeStrYmdhms(timeAfter);
|
|
|
- System.out.println(itemId);
|
|
|
+
|
|
|
+ public static void main(String[] args) throws ParseException {
|
|
|
+ String startTime = "2023-09-27 11:49:41";
|
|
|
+ int time = 5; // 间隔时间为5秒
|
|
|
+
|
|
|
+ System.out.println("秒:"+getCron(startTime, "5", 1, null));
|
|
|
+// System.out.println("分:"+getCron(startTime, "5", 2, null));
|
|
|
+// System.out.println("时:"+getCron(startTime, "5", 3, null));
|
|
|
+// System.out.println("天:"+ getCron(startTime, "11:23:34", 4, null));
|
|
|
+// System.out.println("月:"+getCron(startTime, "11:23:33", 5, 4));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拼接cron表达式
|
|
|
+ * @param date
|
|
|
+ * @param time
|
|
|
+ * @param type
|
|
|
+ */
|
|
|
+ public static String getCron(String date, String time, int type, Integer day) throws ParseException {
|
|
|
+ String cronExpression = "";
|
|
|
+ // 将起始时间转换为Date对象
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date startDate = null;
|
|
|
+ try {
|
|
|
+ startDate = dateFormat.parse(date);
|
|
|
+
|
|
|
+ // 计算Cron表达式的各个字段
|
|
|
+ int second = startDate.getSeconds();
|
|
|
+ int minute = startDate.getMinutes();
|
|
|
+ int hour = startDate.getHours();
|
|
|
+ if (type == 1) {
|
|
|
+ // 秒
|
|
|
+ cronExpression = String.format("%d/%d %d/%d * * * ?", second, Integer.parseInt(time), minute, 1);
|
|
|
+ } else if (type == 2) {
|
|
|
+ // 分
|
|
|
+ cronExpression = String.format("%d %d/%d * * * ?",
|
|
|
+ second, minute, Integer.parseInt(time));
|
|
|
+ } else if (type == 3) {
|
|
|
+ // 时
|
|
|
+ // 计算Cron表达式的小时字段
|
|
|
+ int cronHour = (hour + Integer.parseInt(time)) % 24;
|
|
|
+
|
|
|
+ // 构建Cron表达式
|
|
|
+ cronExpression = String.format("%d %d %d/%d * * ?", second, minute, cronHour, Integer.parseInt(time));
|
|
|
+ } else if (type == 4) {
|
|
|
+ // 每天的哪个时间
|
|
|
+ cronExpression = getDayCron(startDate, time);
|
|
|
+ } else if (type == 5) {
|
|
|
+ // 每月哪个时间
|
|
|
+ cronExpression = getMonCron(startDate, time, day);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println("生成的Cron表达式为:" + cronExpression);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (!"".equals(cronExpression)) {
|
|
|
+ getExpressTime(cronExpression, startDate);
|
|
|
+ }
|
|
|
+ return cronExpression;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDayCron(Date startDate, String time) {
|
|
|
+ String[] times = time.split(":");
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(startDate);
|
|
|
+
|
|
|
+ // 设置每天的执行时间为10:30:21
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
|
|
|
+ calendar.set(Calendar.MINUTE, Integer.parseInt(times[1]));
|
|
|
+ calendar.set(Calendar.SECOND, Integer.parseInt(times[2]));
|
|
|
+
|
|
|
+ // 构建Cron表达式
|
|
|
+ String cronExpression = String.format("%d %d %d * * ?",
|
|
|
+ calendar.get(Calendar.SECOND),
|
|
|
+ calendar.get(Calendar.MINUTE),
|
|
|
+ calendar.get(Calendar.HOUR_OF_DAY));
|
|
|
+ return cronExpression;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMonCron(Date startDate, String time, int day) {
|
|
|
+ String[] times = time.split(":");
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(startDate);
|
|
|
+
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, day);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
|
|
|
+ calendar.set(Calendar.MINUTE, Integer.parseInt(times[1]));
|
|
|
+ calendar.set(Calendar.SECOND, Integer.parseInt(times[2]));
|
|
|
+
|
|
|
+ // 构建Cron表达式
|
|
|
+ String cronExpression = String.format("%d %d %d %d * ?",
|
|
|
+ calendar.get(Calendar.SECOND),
|
|
|
+ calendar.get(Calendar.MINUTE),
|
|
|
+ calendar.get(Calendar.HOUR_OF_DAY),
|
|
|
+ calendar.get(Calendar.DAY_OF_MONTH));
|
|
|
+ return cronExpression;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getExpressTime(String cronExpression, Date currentDate) throws ParseException {
|
|
|
+ CronExpression cron = new CronExpression(cronExpression);
|
|
|
+
|
|
|
+ // 获取下一次执行时间
|
|
|
+ Date nextExecutionTime = cron.getNextValidTimeAfter(currentDate);
|
|
|
+ // 获取上一次执行时间
|
|
|
+ Date prevExecutionTime = calculatePrevExecutionTime(cron, nextExecutionTime);
|
|
|
+
|
|
|
+
|
|
|
+ // 格式化时间
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String formattedNextExecutionTime = dateFormat.format(nextExecutionTime);
|
|
|
+ String formattedPrevExecutionTime = dateFormat.format(prevExecutionTime);
|
|
|
+ System.out.println("当前时间: " + dateFormat.format(currentDate));
|
|
|
+ // 打印结果
|
|
|
+ System.out.println("下一次执行时间: " + formattedNextExecutionTime);
|
|
|
+ System.out.println("上一次执行时间: " + formattedPrevExecutionTime);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Date calculatePrevExecutionTime(CronExpression cron, Date currentDate) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(currentDate);
|
|
|
+
|
|
|
+ // 初始将时间减一秒,以确保不会返回当前时间
|
|
|
+ calendar.add(Calendar.SECOND, -1);
|
|
|
+
|
|
|
+ // 循环查找上一次执行时间
|
|
|
+ while (true) {
|
|
|
+ Date prevTime = calendar.getTime();
|
|
|
+ if (cron.isSatisfiedBy(prevTime)) {
|
|
|
+ return prevTime;
|
|
|
+ }
|
|
|
+ calendar.add(Calendar.SECOND, -1);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|