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));
        });
    }
}
相关推荐
二哈赛车手8 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
为何创造硅基生物8 小时前
C语言 结构体内存对齐规则(通俗易懂版)
c语言·开发语言
吃好睡好便好8 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
栗子~~9 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
星寂樱易李9 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
YDS8299 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
仰泳之鹅9 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
之歆9 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
未若君雅裁10 小时前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
cen__y10 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git