java 获取一天内crontab任务执行的时间点

获取当天crontab任务执行的时间点
网上有如下方法来获取执行时间点

List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, from, to);

但是,貌似这个方法已经过时无法使用了。
可以通过下面的方法,获取当天一整天内任务执行的时间点。

import org.quartz.CronExpression;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by LiChao on 2018/6/1
 */
public class QuartzTest {

    public static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    public static SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws ParseException {
        Date nextTime = df.parse(df2.format(new Date()) + " 00:00:00");
        Date to = new Date(nextTime.getTime() + 24*3600*1000);
        CronExpression expression;
        List<Date> crontimes = new ArrayList<>();
        expression = new CronExpression("0 0/30 9-17 * * ?");
        for(;nextTime.getTime()<=to.getTime();){
            nextTime = expression.getNextValidTimeAfter(nextTime);
            if(nextTime.getTime()>=to.getTime()) break;
            crontimes.add(nextTime);
        }
        for(int i=0;i<crontimes.size();i++){
            System.out.println(df.format(crontimes.get(i)));
        }
    }

}

输出如下:

2018-06-01 09:00:00
2018-06-01 09:30:00
2018-06-01 10:00:00
2018-06-01 10:30:00
2018-06-01 11:00:00
2018-06-01 11:30:00
2018-06-01 12:00:00
2018-06-01 12:30:00
2018-06-01 13:00:00
2018-06-01 13:30:00
2018-06-01 14:00:00
2018-06-01 14:30:00
2018-06-01 15:00:00
2018-06-01 15:30:00
2018-06-01 16:00:00
2018-06-01 16:30:00
2018-06-01 17:00:00
2018-06-01 17:30:00

Process finished with exit code 0

引入依赖包

<dependency>
      <groupId>opensymphony</groupId>
      <artifactId>quartz-all</artifactId>
      <version>1.6.3</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值