Verifying a cron expression is valid in Java

前端 未结 6 1340
梦谈多话
梦谈多话 2020-12-15 03:08

I\'m writing a scheduling application in Java using Quartz. I\'m using the CronTrigger, but my cron expressions are entered into a database before they are scheduled and are

6条回答
  •  天命终不由人
    2020-12-15 03:52

    If you are using org.quartz, you can validate a Cron expression as follows:

    try {
        CronExpression cron = new CronExpression(cronExpression);
        ...
    } catch (ParseException e) {
        //exception handling
    }
    

    Quoted from official API document Class CronExpression:

    public CronExpression(String cronExpression) throws ParseException
    Constructs a new CronExpression based on the specified parameter.
    Parameters:
    cronExpression - String representation of the cron expression the new object should represent
    Throws:
    ParseException - if the string expression cannot be parsed into a valid CronExpression

提交回复
热议问题