You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.5 KiB
64 lines
1.5 KiB
11 months ago
|
package com.ruoyi.quartz.util;
|
||
|
|
||
|
import java.text.ParseException;
|
||
|
import java.util.Date;
|
||
|
import org.quartz.CronExpression;
|
||
|
|
||
|
/**
|
||
|
* cron表达式工具类
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*/
|
||
|
public class CronUtils
|
||
|
{
|
||
|
/**
|
||
|
* 返回一个布尔值代表一个给定的Cron表达式的有效性
|
||
|
*
|
||
|
* @param cronExpression Cron表达式
|
||
|
* @return boolean 表达式是否有效
|
||
|
*/
|
||
|
public static boolean isValid(String cronExpression)
|
||
|
{
|
||
|
return CronExpression.isValidExpression(cronExpression);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 返回一个字符串值,表示该消息无效Cron表达式给出有效性
|
||
|
*
|
||
|
* @param cronExpression Cron表达式
|
||
|
* @return String 无效时返回表达式错误描述,如果有效返回null
|
||
|
*/
|
||
|
public static String getInvalidMessage(String cronExpression)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
new CronExpression(cronExpression);
|
||
|
return null;
|
||
|
}
|
||
|
catch (ParseException pe)
|
||
|
{
|
||
|
return pe.getMessage();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 返回下一个执行时间根据给定的Cron表达式
|
||
|
*
|
||
|
* @param cronExpression Cron表达式
|
||
|
* @return Date 下次Cron表达式执行时间
|
||
|
*/
|
||
|
public static Date getNextExecution(String cronExpression)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
CronExpression cron = new CronExpression(cronExpression);
|
||
|
return cron.getNextValidTimeAfter(new Date(System.currentTimeMillis()));
|
||
|
}
|
||
|
catch (ParseException e)
|
||
|
{
|
||
|
throw new IllegalArgumentException(e.getMessage());
|
||
|
}
|
||
|
}
|
||
|
}
|