大家好,又见面了,我是你们的朋友全栈君。
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
看,简单吧!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149579.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...