Verifying a cron expression is valid in Java

Omar Kooheji picture Omar Kooheji · Mar 2, 2010 · Viewed 40.5k times · Source

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 based on user input.

Is there a way I can verify that the cron expressions are valid when I capture them? I'd rather do this and give the user an appropriate error message than wait until the scheduler is run and I get a ParseException when I try and create the trigger. Which could be days after the user inputs the data.

Answer

sfussenegger picture sfussenegger · Mar 2, 2010

Can't you simply create a trigger without actually executing it? You could simply give appropriate feedback in case of a ParseException. If the expression is okay, persist the expression to DB.

Edit: or simply do this:

org.quartz.CronExpression.isValidExpression(expression);