当前位置:网站首页>Time to calculate cron expression based on cronsequencegenerator

Time to calculate cron expression based on cronsequencegenerator

2022-07-05 14:12:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

Spring Provides CronSequenceGenerator Class for calculation cron The specific time point of the expression next Method (Spring3.2 edition ) and cron Is it legal isValidExpression Method (Spring4.3 edition ).

Practical examples :

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 * * * ?"; // Every five minutes 

        CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);

        Date currentTime = new Date();

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

        Date nextTimePoint = cronSequenceGenerator.next(currentTime); // currentTime To calculate the start time of the next time point 
        System.out.println("nextTimePoint: " + nextTimePoint);

        Date nextNextTimePoint = cronSequenceGenerator.next(nextTimePoint);

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

Output results :

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

see , Easy !

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/149579.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051353303693.html