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));
        });
    }
}
相关推荐
星期八不上发条4 小时前
智能指针是什么?使用场景,循环引用解决办法,面试回答
开发语言·c++·stl
OKkankan5 小时前
Python 基础进阶(三):从函数、类到 asyncio 与 FastAPI 后端开发实战
开发语言·python
mldong5 小时前
弃用 ZCode,转 DeepSeek Harness:我用 GitHub Actions 自建 Windows 打包的全实录
java·架构
Wang's Blog5 小时前
Java 接入Redis: Redis下载与源码编译安装
java·服务器·redis
农民工老王5 小时前
故障排查:反向代理转发 GET 请求时 JDK 自动追加 Content-Length:0 致后端 500
java·httpclient·content-length
泡海椒5 小时前
jquick-pdf 防止 PDF 内容分页断裂:keepTogether 属性妙用
java·开发语言·pdf
vx_BS813306 小时前
【项目编号:project51381】Spring Boot 婚纱摄影管理系统:套餐预约、在线咨询、支付与客片展示的一站式业务实现
java·spring boot·eclipse·tomcat·mybatis
IT毕设实战小研6 小时前
基于大数据的商场商铺数据分析与可视化的设计与实现
android·java·大数据·django·课程设计
万年咸鱼6 小时前
Java Classpath 详解:从原理到实战
java