Dhruvam Sharma
Dhruvam Sharma

Reputation: 1792

How to write a cron expression for 10th weekday of the month?

I am a novice in the field pf CRON Expression. I want to write one for the 10th Workday (Business Day) of the month.

I have already tried out this site to help me out with the expressions. Couldn't help myself with this.

Upvotes: 1

Views: 2821

Answers (3)

this_is_om_vm
this_is_om_vm

Reputation: 636

Try with below cron expression it will execute on every 10th day of month.If it is weekday then only cron will work as I have assumed (MON-FRI) as business days.Please try and ask for further any help

@Scheduled(cron = "0 0 12 10 1-5 ?")
public void scheduletask() {
    // ...
}

thnkx.

Upvotes: 0

Sofo Gial
Sofo Gial

Reputation: 703

You can create a CRON job that runs daily. You can then nest your functionality in an if clause, such as:

if (isTenthWeekday()) {
    //your functionality
}

private boolean isTenthWeekday() {
    Calendar c = Calendar.getInstance();
    int sumWeekdays = 0;
    for (int i=1; i<=c.get(Calendar.DAY_OF_MONTH); i++) {
        Calendar d = Calendar.getInstance();
        d.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), i);
        if (d.get(Calendar.DAY_OF_WEEK) > 1 && d.get(Calendar.DAY_OF_WEEK) <= 6) {
            sumWeekdays++;
        }
    }
    return sumWeekdays == 10;
}

Upvotes: 2

Simon
Simon

Reputation: 965

+---------------- minute (0 - 59)
|  +------------- hour (0 - 23)
|  |  +---------- day of month (1 - 31)
|  |  |  +------- month (1 - 12)
|  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
|  |  |  |  |
*  *  *  *  *  command to be executed

This is the matrix. For the use of workdays (maybe exclude holidays etc. you need to rewrite the script for these days)

Upvotes: 2

Related Questions