Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz cron schedule output not as expected

I want to schedule a daily job at 23:59:59 only in weekdays (monday - friday).

i use this cron expression

"59 59 23 ? * MON-FRI",

but the output has tripe value for monday

Wed Aug 29 23:59:59 ICT 2012
Thu Aug 30 23:59:59 ICT 2012
Fri Aug 31 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012

Tue Sep 04 23:59:59 ICT 2012
Wed Sep 05 23:59:59 ICT 2012
Thu Sep 06 23:59:59 ICT 2012
Fri Sep 07 23:59:59 ICT 2012

is the expression wrong? need help.

i'm getting this output by loop through specific date, here the code

`try {
    CronExpression ce = new CronExpression(59 59 23 ? * MON-FRI);

    Calendar start = Calendar.getInstance();
    start.setTime(new Date());

    Calendar end = Calendar.getInstance();
    Date endDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse("Fri Sep 29 23:59:59 ICT 2012");
            end.setTime(endDate);

    for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
       Date current = start.getTime();

         System.out.println(ce.getNextValidTimeAfter(current));

     }
 } catch (ParseException ex) {
            Logger.getLogger(HelloJob.class.getName()).log(Level.SEVERE, null, ex);
        }

}`
like image 549
user1490927 Avatar asked Dec 31 '25 21:12

user1490927


1 Answers

The problem isn't in you rule or in Quartz, it's OK and you may use it.

The problem is in your test code.

for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
     Date current = start.getTime();
     System.out.println(ce.getNextValidTimeAfter(current));
}

You're not iterating on valid dates but on all days between startDate and endDate.

The loop content is called for invalid days too and for each of those 2 invalid days the "next valid time" after current date is monday. So you have thrice monday, that's perfectly logic.

Hence your log.

like image 59
Denys Séguret Avatar answered Jan 03 '26 11:01

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!