根据CronSequenceGenerator计算cron表达式的时间

        Spring提供了CronSequenceGenerator类以便计算cron表达式的具体时间点的next方法(Spring3.2版本)和cron是否合法的isValidExpression方法(Spring4.3版本)。

实战举例:

package construct;

import org.springframework.scheduling.support.CronSequenceGenerator;

import java.util.Date;

/**
 * Created by zhangzh on 2017/1/18.
 */
public class CronSequenceGeneratorTest {

    public static void main(String[] args) {

        String cron = "0 */5 * * * ?"; //每个五分钟执行一次

        CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);

        Date currentTime = new Date();

        System.out.println("currentTime: " + currentTime);

        Date nextTimePoint = cronSequenceGenerator.next(currentTime); // currentTime为计算下次时间点的开始时间
        System.out.println("nextTimePoint: " + nextTimePoint);

        Date nextNextTimePoint = cronSequenceGenerator.next(nextTimePoint);

        System.out.println("nextNextTimePoint: " + nextNextTimePoint);
    }
}


输出结果:


currentTime: Wed Jan 18 19:45:25 CST 2017
nextTimePoint: Wed Jan 18 19:50:00 CST 2017
nextNextTimePoint: Wed Jan 18 19:55:00 CST 2017

看,简单吧!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值