Quartz:每周一次的cron表达

时间:2016-03-18 10:58:07

标签: java cron expression

我无法找到一个有效的cron表达式:

  • 每X周运行一次(案例“semaine”)

不要忘记考虑开始日期!

private static String generateCronExpression(Date startTime, String periodicity, long value) throws Exception {
    String second = new SimpleDateFormat("s").format(startTime);
    String minute = new SimpleDateFormat("m").format(startTime);
    String hour = new SimpleDateFormat("HH").format(startTime);
    String day = new SimpleDateFormat("dd").format(startTime);
    String month = new SimpleDateFormat("M").format(startTime);

    switch (periodicity) {
    case "seconde":
        return second + "/" + value + " * * 1/1 * ? *";
    case "minute":
        return second + " 0/" + value + " * 1/1 * ? *";
    case "heure":
        return second + " " + minute + " 0/" + value + " 1/1 * ? *";
    case "jour":
        return second + " " + minute + " " + hour + " " + day + "/" + value + " * ? *";
    case "semaine":
        return second + " " + minute + " " + hour + " " + day + "/" + (7 * value) + " * ? *";
    case "mois":
        return second + " " + minute + " " + hour + " " + day + " 1/" + value + " ? *";
    case "annee":
        return second + " " + minute + " " + hour + " " + day + " " + month + " ? " + "0/" + value;
    default:
        throw new Exception("[generateCronExpression] - Unknown periodicity");
    }
}

0 个答案:

没有答案