Search code examples
javaquartz-schedulercronexpression

java.lang.RuntimeException: CronExpression '4 27 11 ? 8 ? 2014' is invalid,


Getting this as an invalid CronExpression, can't figure out why

Refferred http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

This is how I am generating the Cron Expression:

public class sample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Date date = new Date();
        String formatted_date = generateCronExpression(Integer.toString(date.getSeconds()),Integer.toString(date.getMinutes()),
                Integer.toString(date.getHours()), "?", Integer.toString(date.getMonth()), "?", Integer.toString(date.getYear()+1900));

    }
    private static String generateCronExpression(final  String seconds,final String minutes, final String hours, final String dayOfMonth, final String month, final String dayOfWeek, final String year) {
        return String.format("%1$s %2$s %3$s %4$s %5$s %6$s %7$s", seconds,minutes, hours, dayOfMonth, month, dayOfWeek, year);
    }
}

Solution

  • '?' in a cron expression is meant to allow day-of-month and day-of-week to not interfere with each other (e.g., so you can specify a cron to trigger on any Friday regardless of the day of the month or on the 13th of every month, regardless of which day it is). If you specify both of them to be '?' you don't have any date specification, which would be illegal.

    A cron expression for the current date would use the day of the month, and ignore the day of the week. E.g., for today, September 15th, 2014, you'd specify 4 27 11 15 9 ? 2014.

    This can be generated by extracting the current day from the java Date object:

    public static void main(String[] args) {
        Date date = new Date();
        String formatted_date = generateCronExpression
                                 (Integer.toString(date.getSeconds()),
                                  Integer.toString(date.getMinutes()),
                                  Integer.toString(date.getHours()),
                                  Integer.toString(date.getDate()),
                                  Integer.toString(date.getMonth() + 1), // see Note #2
                                  "?",
                                  Integer.toString(date.getYear() + 1900));
    }
    

    Notes:

    1. Date.getDate(), Date.getHours() etc. are deprecated - you should use Calendar.get instead. I kept the current code from the OP in order to make the solution clear and not to add clutter with extra details.
    2. Date.getMonth() (and the new recommended method, Calendar.get(Calendar.MONTH)) return a zero-based representation of the month (e.g., January is 0, February is 1, etc.), while cron expressions are one based (e.g., January is 1, February is 2, etc) - so you should add 1 for the cron-expression.