1

I am using java spring boot. I need to fetch data which updates as "Data sets are updated quarterly (March, June, September, December), typically on the first Friday of the month"

And I want to run Cron job on next Sunday (want to keep one entire saturday as buffer) after that. What will be Crone expression for this condition?

Reference for cron job. https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm

Reference for spring boot cron job. https://www.tutorialspoint.com/spring_boot/spring_boot_scheduling.htm

Anonymous Creator
  • 2,968
  • 7
  • 31
  • 77
  • https://crontab.guru/ Does this site help? – grian Jan 24 '21 at 20:44
  • 1
    Thanks that is very nice tool. Still I am not able to figure out my need. What I have started realizing is, this case might not be possible at all, and I simply have to set with 9th day of the month. That is 7 days + 1 day buffer and check after that buffer day which is 9th day. – Anonymous Creator Jan 25 '21 at 05:48

1 Answers1

1

Special cases we'll need to address: When Sunday is day #1 or day #2 of the month. By limiting Sundays to days in the range 3-9, we're safe. When Sunday is day #3 or greater, we can know for sure that there is a preceding Friday in current month.

Expression

0 0 16 3-9 3/3 SUN

Expression will fire at

Sun, Mar 7, 2021 at 04:00:00 pm
Sun, Jun 6, 2021 at 04:00:00 pm
Sun, Sep 5, 2021 at 04:00:00 pm
Sun, Dec 5, 2021 at 04:00:00 pm
Sun, Mar 6, 2022 at 04:00:00 pm

Expression may be tested like this:

    var sundays = CronExpression.parse("0 0 16 3-9 3/3 SUN");
    var nextDate = LocalDateTime.now();
    var dateFormatter = DateTimeFormatter.ofPattern("EEE, MMM d, yyyy 'at' hh:mm:ss a");

    for (var i = 0; i < 5; i++) {
        nextDate = sundays.next(nextDate);
        System.out.println(nextDate.format(dateFormatter));
    }

We can verify that the special-case is working as depicted by having a look into June 2024

Sun, Jun 9, 2024 at 04:00:00 pm

From comment from OP

We can verify this case in May 2021. where saturday is first day of month. So instead of 2nd May Sunday, we need 9th May Sunday. Which is sunday after first Friday of month.

May 2021 is outside the quarterly schedule specified in question, but we can test this case by locking month to May and keep the rest of the expression unchanged like this:

0 0 16 3-9 5 SUN

Test code above will return Sun, May 9, 2021 at 04:00:00 pm which is what OP is asking for.

Roar S.
  • 8,103
  • 1
  • 15
  • 37
  • It is neither first friday nor first sunday, But it is next sunday of first friday. – Anonymous Creator Jan 25 '21 at 05:19
  • We can verify this case in May 2021. where saturday is first day of month. So instead of 2nd May Sunday, we need 9th May Sunday. Which is sunday after first Friday of month. – Anonymous Creator Jan 25 '21 at 05:45
  • @AnonymousCreator: For testing your case from comment with May 2021: `0 0 16 3-9 5 SUN` If you put this expression into my test code, you'll get `Sun, May 9, 2021 at 04:00:00 pm` which is what you're asking for. BR – Roar S. Jan 25 '21 at 15:57
  • 1
    Thanks for that. I haven't verified yet. But logic seems to be should work. – Anonymous Creator Jan 26 '21 at 14:46
  • 1
    @AnonymousCreator: You're welcome. I've tested this quite thoroughly, but if you'll experience any problems, please let me know. BR – Roar S. Jan 26 '21 at 14:49