gpt4 book ai didi

java - 使用 Quartz 每 30 天的月度作业

转载 作者:搜寻专家 更新时间:2023-11-01 01:55:17 26 4
gpt4 key购买 nike

伙计们,

我有用户安排的每月作业(使用 Quartz)。用户提供开始日期 f或第一个运行的作业,它可以是每月 1-31 日的任何一天

我的问题是如何使用 cron 触发器来安排这个,请记住并非所有月份都有第 31、30、29 天。在这种情况下,作业应该在最接近该月的前一天运行。所以,假设 4 月只有 30 天,那么作业必须在 4 月 30 日运行。

可以使用单个 cron 触发器来完成吗?或者它应该是触发器的组合?我尝试使用 CronExpression 来了解它如何处理此类情况:

CronExpression ce = new CronExpression("0 0 0 30 JAN-DEC ? *");
Date nextValidTime = ce.getNextValidTimeAfter(//**27th of February**//);

我的 nextValidTime 等于 3 月 30 日,所以 cron 只是“跳过”了 2 月。任何帮助将不胜感激。提前致谢。

最佳答案

The 'L' character is allowed for the day-of-month and day-of-week fields. This character > is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". You can also specify an offset from the last day of the month, such as "L-3" which would mean the third-to-last day of the calendar month. When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing/unexpected results.

http://quartz-scheduler.org/api/2.0.0/org/quartz/CronExpression.html

new CronExpression("0 0 0 L JAN-DEC ? *");

编辑:

那我就做这样的事

Calendar tCalendar = Calendar.getInstance();
tCalendar.set(2009, Calendar.FEBRUARY/*int*/, 1); // for example Feb, 2009 -- day doesn't matter here
if(userSelectedDay > tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) ){
//Fix user day
//fixedDay = tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)

// Or, for that month
//new CronExpression("0 0 0 L JAN-DEC ? *");
}

关于java - 使用 Quartz 每 30 天的月度作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11442283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com