Cron4j pattern matching precise hour while it shouldn't - cronexpression

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 * * * *

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)

Cron/Quartz Every N months

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?

later js: every year on same date wrong response

I am trying to create a schedule in laterjs for next 10 occurrence at a specified time for every year.
Following is my code:
var later = require("later")
var startdate = new Date();
var curr_time = "12:09:10";
later.date.localTime();
var s = later.parse.recur().every(1).year().on(curr_time).time();
later.schedule(s).next(5, startdate);
Response is :
Array (5 items)
0: Thu Aug 17 2017 12:09:10 GMT+0530 (India Standard Time)
1: Fri Aug 18 2017 12:09:10 GMT+0530 (India Standard Time)
2: Sat Aug 19 2017 12:09:10 GMT+0530 (India Standard Time)
3: Sun Aug 20 2017 12:09:10 GMT+0530 (India Standard Time)
4: Mon Aug 21 2017 12:09:10 GMT+0530 (India Standard Time)
please tell me, where i am doing wrong.

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

Resources