Cron/Quartz Every N months - cron

The cron expression:
0 0 0 L 8/7 ?
Is described by this site as:
"At 00:00:00am, on the last day of the month, every 7 months starting in August"
However, when I choose to see the next execution dates I get:
Thu Aug 31 00:00:00 UTC 2017
Fri Aug 31 00:00:00 UTC 2018
Sat Aug 31 00:00:00 UTC 2019
Mon Aug 31 00:00:00 UTC 2020
Tue Aug 31 00:00:00 UTC 2021
Wed Aug 31 00:00:00 UTC 2022
Thu Aug 31 00:00:00 UTC 2023
Sat Aug 31 00:00:00 UTC 2024
Which really is "every 12 months starting in August". I was expecting to get a 7-month period, but I suspect everything resets when you cross the January threshold.
Is there any way I can actually get periodic values such as every n months or every n weeks or is that not really possible out of the box with cron?

Related

Quartz Cron Expression Issue # Monthly Jobs Failing

I have a use case where i want to run a monthly job starting at 2:30 PM for every first friday of every month starting from january.
Cron expression which i use :-
0 30 14 ? 1/1 6#1
This works absolutely fine.
Sample fire times : -
Fri Jan 03 14:30:00 UTC 2020
Fri Feb 07 14:30:00 UTC 2020
Fri Mar 06 14:30:00 UTC 2020
Fri Apr 03 14:30:00 UTC 2020
Fri May 01 14:30:00 UTC 2020
Fri Jun 05 14:30:00 UTC 2020
Fri Jul 03 14:30:00 UTC 2020
But if i use the same expression and use December as the starting month
0 30 14 ? 12/1 6#1
This starts failing :-
Fri Dec 04 14:30:00 UTC 2020
Fri Dec 03 14:30:00 UTC 2021
Fri Dec 02 14:30:00 UTC 2022
Fri Dec 01 14:30:00 UTC 2023
This kind of becomes yearly.
I don't see any issue with the expression i am using.How do we resolve this or a workaround ?
IMO this mean every 12 month = every December and it is the same as
0 30 14 ? 12 6#1
and your first record is equal to
0 30 14 ? * 6#1
(star mean every month)

how get week numbers starting from sunday

Date Day_name ****WeekNum****
1/1/2016 Fri 1
1/2/2016 Sat 1
1/3/2016 Sun 2
1/4/2016 Mon 2
1/5/2016 Tue 2
1/6/2016 Wed 2
1/7/2016 Thu 2
1/8/2016 Fri 2
1/9/2016 Sat 2
1/10/2016 Sun 3
1/11/2016 Mon 3
1/12/2016 Tue 3
1/13/2016 Wed 3
1/14/2016 Thu 3
1/15/2016 Fri 3
1/16/2016 Sat 3
1/17/2016 Sun 4
1/18/2016 Mon 4
1/19/2016 Tue 4
1/20/2016 Wed 4
1/21/2016 Thu 4
1/22/2016 Fri 4
1/23/2016 Sat 4
1/24/2016 Sun 5
1/25/2016 Mon 5
1/26/2016 Tue 5
1/27/2016 Wed 5
1/28/2016 Thu 5
1/29/2016 Fri 5
1/30/2016 Sat 5
1/31/2016 Sun 6
2/1/2016 Mon 6
2/2/2016 Tue 6
2/3/2016 Wed 6
Here Your starting week day should begin with sunday and the end day of week should be saturday
Assuming you have your date in A1 your B1 and C1 are as follows:
Day_name is =TEXT(A1,"ddd")
WeekNum starting on Sunday is =WEEKNUM(A1,1)

Grails get day of current week and last three weeks

I got a domain work with id, day
Day shows value from Match to current.
I need to find the list of current week and last two weeks
Ex: today is Monday (04/22) then what I need is:
Week1: 06-12 April
Week2: 13-19 April
Current week: 20-26 April.
Please helps, thanks.
Posted here for posterity:
def current = new Date().clearTime()
int currentDay = Calendar.instance.with {
time = current
get( Calendar.DAY_OF_WEEK )
}
def listOfDays = (current - 13 - currentDay)..(current + 7 - currentDay)
listOfDays.each {
println it
}
Prints:
Sun Apr 06 00:00:00 BST 2014
Mon Apr 07 00:00:00 BST 2014
Tue Apr 08 00:00:00 BST 2014
Wed Apr 09 00:00:00 BST 2014
Thu Apr 10 00:00:00 BST 2014
Fri Apr 11 00:00:00 BST 2014
Sat Apr 12 00:00:00 BST 2014
Sun Apr 13 00:00:00 BST 2014
Mon Apr 14 00:00:00 BST 2014
Tue Apr 15 00:00:00 BST 2014
Wed Apr 16 00:00:00 BST 2014
Thu Apr 17 00:00:00 BST 2014
Fri Apr 18 00:00:00 BST 2014
Sat Apr 19 00:00:00 BST 2014
Sun Apr 20 00:00:00 BST 2014
Mon Apr 21 00:00:00 BST 2014
Tue Apr 22 00:00:00 BST 2014
Wed Apr 23 00:00:00 BST 2014
Thu Apr 24 00:00:00 BST 2014
Fri Apr 25 00:00:00 BST 2014
Sat Apr 26 00:00:00 BST 2014

Groovy, get list of current week and last 2 weeks

I got a domain work with id, day, list day from January to now.
I get the current time by code:
def current = new Date()
So, I'd like to get list day from last 2 weeks, included this week, then I used the following code but it doesn't work.
def getWeek = current.Time - 13 (13 is 2 week + today)
Please help me solve it.
Not 100% sure I understand, but you should be able to use a Range:
def current = new Date().clearTime()
def listOfDays = (current - 13)..current
listOfDays.each { println it }
That prints:
Wed Apr 09 00:00:00 BST 2014
Thu Apr 10 00:00:00 BST 2014
Fri Apr 11 00:00:00 BST 2014
Sat Apr 12 00:00:00 BST 2014
Sun Apr 13 00:00:00 BST 2014
Mon Apr 14 00:00:00 BST 2014
Tue Apr 15 00:00:00 BST 2014
Wed Apr 16 00:00:00 BST 2014
Thu Apr 17 00:00:00 BST 2014
Fri Apr 18 00:00:00 BST 2014
Sat Apr 19 00:00:00 BST 2014
Sun Apr 20 00:00:00 BST 2014
Mon Apr 21 00:00:00 BST 2014
Tue Apr 22 00:00:00 BST 2014
If you mean you want the entire 2 weeks before the current week AND the current week, you could do:
def current = new Date().clearTime()
int currentDay = Calendar.instance.with {
time = current
get( Calendar.DAY_OF_WEEK )
}
def listOfDays = (current - 13 - currentDay)..(current + 7 - currentDay)
listOfDays.each {
println it
}
Which prints:
Sun Apr 06 00:00:00 BST 2014
Mon Apr 07 00:00:00 BST 2014
Tue Apr 08 00:00:00 BST 2014
Wed Apr 09 00:00:00 BST 2014
Thu Apr 10 00:00:00 BST 2014
Fri Apr 11 00:00:00 BST 2014
Sat Apr 12 00:00:00 BST 2014
Sun Apr 13 00:00:00 BST 2014
Mon Apr 14 00:00:00 BST 2014
Tue Apr 15 00:00:00 BST 2014
Wed Apr 16 00:00:00 BST 2014
Thu Apr 17 00:00:00 BST 2014
Fri Apr 18 00:00:00 BST 2014
Sat Apr 19 00:00:00 BST 2014
Sun Apr 20 00:00:00 BST 2014
Mon Apr 21 00:00:00 BST 2014
Tue Apr 22 00:00:00 BST 2014
Wed Apr 23 00:00:00 BST 2014
Thu Apr 24 00:00:00 BST 2014
Fri Apr 25 00:00:00 BST 2014
Sat Apr 26 00:00:00 BST 2014

Cron4j pattern matching precise hour while it shouldn't

Please consider the following code
String pattern = "*/17 * * * *";
Date d = new Date();
for (int i = 0; i < 10; i++) {
System.out.println("Start: " + d + ", " + (d = new Predictor(pattern, d).nextMatchingDate()));
}
This outputs the following sample (I put the times I didn't expect between brackets):
Start: Tue Jun 11 12:54:48 GMT+02:00 2013, Tue Jun 11 (13:00:00) GMT+02:00 2013
Start: Tue Jun 11 13:00:00 GMT+02:00 2013, Tue Jun 11 13:17:00 GMT+02:00 2013
Start: Tue Jun 11 13:17:00 GMT+02:00 2013, Tue Jun 11 13:34:00 GMT+02:00 2013
Start: Tue Jun 11 13:34:00 GMT+02:00 2013, Tue Jun 11 13:51:00 GMT+02:00 2013
Start: Tue Jun 11 13:51:00 GMT+02:00 2013, Tue Jun 11 (14:00:00) GMT+02:00 2013
Start: Tue Jun 11 14:00:00 GMT+02:00 2013, Tue Jun 11 14:17:00 GMT+02:00 2013
Start: Tue Jun 11 14:17:00 GMT+02:00 2013, Tue Jun 11 14:34:00 GMT+02:00 2013
Start: Tue Jun 11 14:34:00 GMT+02:00 2013, Tue Jun 11 14:51:00 GMT+02:00 2013
Start: Tue Jun 11 14:51:00 GMT+02:00 2013, Tue Jun 11 (15:00:00) GMT+02:00 2013
Start: Tue Jun 11 15:00:00 GMT+02:00 2013, Tue Jun 11 15:17:00 GMT+02:00 2013
Although the pattern is configured to match minutes divisible by 17, if that's a correct way to describe it, the predictor considers a precise hour (13:00, 14:00, 15:00..etc) a valid next-matching-date !
My questions are:
Is this behavior valid ? If yes, then why is it valid ?
How can I prevent this behavior ? I mean how can I prevent the predictor from considering a precise hour as a matching time if the time field (i.e. minutes, hours, days, week day) isn't divisible by it's maximum value (i.e. 60, 24, 30 or 31, 7) ?
Is this actually the case with unix cron scheduling ?
Thank you.
In this context 0 and * are interchangable. So it will run on the hour and then at 17 minute intervals during the hour.
To start at 17 minutes past the hour and then at that duration try this pattern: 17-59/17 * * * *

Resources