根据cron表达式获取下次执行时间

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.scheduling.support.CronSequenceGenerator;

import java.util.Date;

public class TestClass {

    public static void main(String[] args) {
        String cronExp="0 30 9 * * ?";
        // 解析cron表达式
        final CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExp);
        Date now = new Date();
        // 任务下次执行时间
        Date nextTime = cronSequenceGenerator.next(now);
        // 任务下下次执行时间
        Date nextNextTime = cronSequenceGenerator.next(nextTime);
        System.out.println(StrUtil.format("当前时间:{}\n任务下次执行时间:{}\n任务下下次执行时间:{}",
                DateUtil.formatDateTime(now), DateUtil.formatDateTime(nextTime), DateUtil.formatDateTime(nextNextTime)));
    }
}

 

 

 

posted on 2022-01-06 17:42  法哈席基  阅读(2237)  评论(1编辑  收藏  举报