Java 根据cron表达式,获取quartz job执行时间

1.借助quartz中的工具类TriggerUtils,获取当前和下一次触发时间.

TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, fromDate ,toDate)

2.测试code(前提: 需要导入quartz依赖)

复制代码
package boot.spring.quartz;

import org.quartz.TriggerUtils;
import org.quartz.impl.triggers.CronTriggerImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

public class CronUtils {
    
    public static void main(String[] args) throws ParseException {

        CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
        cronTriggerImpl.setCronExpression("0 0/30 * * * ?");// 30分钟执行一次

        OffsetDateTime odt = OffsetDateTime.now();
        ZoneOffset zoneOffset = odt.getOffset();
        LocalDateTime now = LocalDateTime.now();
        // 当前触发的时间
        LocalDateTime lastTime = now.minusMinutes(30);
        LocalDateTime nextTime = now.plusMinutes(30);
        Date lastDate = Date.from(lastTime.toInstant(ZoneOffset.of(zoneOffset.toString())));
        Date nextDate = Date.from(nextTime.toInstant(ZoneOffset.of(zoneOffset.toString())));
        //
        List lastAndNextTimes = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, lastDate ,nextDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        lastAndNextTimes.forEach(date -> {
            System.out.println(dateFormat.format(date));
        });
    }
}
相关推荐
wuqingshun3141591 天前
MySQL 如何解决深度分页问题?
java·mysql·面试
buhuizhiyuci1 天前
【算法篇】位运算 —— 基础篇
java·数据库·算法
laboratory agent开发1 天前
企业AI Agent落地前,先回答四个工程问题
java·前端·人工智能
微三云 - 廖会灵 (私域系统开发)1 天前
电商系统国际化架构设计:多语言、多币种、多时区、多税制的全链路实现
java·前端·数据库
XS0301061 天前
Spring AI 第二课-流式输出 & 运行时动态参数配置
java·人工智能·spring
孬甭_1 天前
C++ string类
开发语言·c++
额恩661 天前
阶段五:HttpOnly Cookie 登录持久化
java·spring
Python图像识别-11 天前
基于yolov8的钢铁缺陷检测系统-2027毕业版(UI界面+Python项目源码+模型+标注好的数据集)
开发语言·python·yolo
sycmancia1 天前
Qt——多线程与界面组件的通信
开发语言·qt·算法
你驴我1 天前
WhatsApp 多账号消息路由中的去重与顺序保证实践
开发语言·php