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));
        });
    }
}
相关推荐
皮皮林5511 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河2 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程4 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅6 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者7 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺7 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart8 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP9 小时前
MyBatis-mybatis入门与增删改查
java
孟陬13 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端