3
votes

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

}`
1
Which cron library are you using?Peter Lawrey
Your rule seems fine for Quartz. How did you get this output ?Denys Séguret

1 Answers

2
votes

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.