如何编写Cron表达式以在每个月的第3个星期日晚上11点执行触发器?

Har*_*ari 1 java spring spring-mvc

我想在每个月的第3个星期日发射扳机.在cron表达式中我使用了cron ="0 0 23?*1#3"但是它给了我Exception

java.lang.NumberFormatException: For input string: "1#3"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.valueOf(Unknown Source)
    at org.springframework.scheduling.support.CronSequenceGenerator.getRange(CronSequenceGenerator.java:324)
    at org.springframework.scheduling.support.CronSequenceGenerator.setNumberHits(CronSequenceGenerator.java:297)
    at org.springframework.scheduling.support.CronSequenceGenerator.setDays(CronSequenceGenerator.java:275)
    at org.springframework.scheduling.support.CronSequenceGenerator.parse(CronSequenceGenerator.java:241)
    at org.springframework.scheduling.support.CronSequenceGenerator.<init>(CronSequenceGenerator.java:81)
    at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:54)
    at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:44)
    at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:188)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:209)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:1)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1525)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1515)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试的代码

@Scheduled(cron="0 0 23 ? * 1#3") // Fire at 11 PM on the third sunday of every month
    public void sendReportNotCreatedNotificationToStudent() throws Exception{
        scheduleNotificationIntf.sendScheduleNotificationToStudent("createReportRemainder.html");
    }
Run Code Online (Sandbox Code Playgroud)

请有人告诉这个错误.我怎样才能实现这个Cron表达式.

Bar*_*erk 5

我可能会来晚一点,但我碰巧遇到了同样的问题,而且我已经找到了一个解决方法.我测试了它,它的工作原理!这个表达式:

@Scheduled(cron="0 0 23 1-7 * WED") // First wednesday of the month at 23:00
Run Code Online (Sandbox Code Playgroud)

将在本月的第一个星期三举行.解释很简单:第一个星期三将始终是1-7范围内的一天,所以如果我们过滤当月的日期在1到7之间,而星期几到星期三,我们就完成了:)我没有测试它,但使用相同的逻辑,这些表达式应该也可以正常工作:

@Scheduled(cron="0 0 23 8-14 * WED") // Second wednesday of the month at 23:00
@Scheduled(cron="0 0 23 15-21 * WED") // Third wednesday of the month at 23:00
@Scheduled(cron="0 0 23 22-28 * WED") // Fourth wednesday of the month at 23:00
@Scheduled(cron="0 0 23 29-31 * WED") // Fifth wednesday of the month at 23:00
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助!