java.lang.RuntimeException:CronExpression'4 27 11? 8? 2014'无效, [英] java.lang.RuntimeException: CronExpression '4 27 11 ? 8 ? 2014' is invalid,

查看:5785
本文介绍了java.lang.RuntimeException:CronExpression'4 27 11? 8? 2014'无效,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将此作为无效的CronExpression无法弄清楚为什么

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

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

这是我生成Cron表达式的方式:

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);
    }
}


推荐答案

?在cron表达式中意味着允许日期和星期几不相互干扰(例如,因此您可以指定cron在任何星期五触发,而不考虑每月的日期或每月13日,不管是哪一天)。如果您同时指定两者都是?,则您没有任何日期说明,这是非法的。

'?' 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.

当前日期的cron表达式将使用日期,并忽略一周中的某一天。例如,今天,2014年9月15日,您会指定 4 27 11 15 9? 2014

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.

这可以通过从java Date 对象:

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));
}

注:


  1. Date.getDate() Date.getHours 等已被弃用 - 您应该使用 Calendar.get

  2. Date.getMonth()(和新的推荐方法 Calendar.get(Calendar.MONTH))返回一个基于零的表示形式的月(例如,一月是0,二月是1,等等),而cron表达式是基于一个(例如,一月是1,二月是2,等) - 所以你应该为cron表达式添加1.

  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.

这篇关于java.lang.RuntimeException:CronExpression'4 27 11? 8? 2014'无效,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆