0

Wondering how can I check if a date/time range satisfies a cron expression in Java. For example, if I say I have time range from

Monday 12:00 AM to 7:00 AM

and mention cron 0 0 6 * * MON, the comparison should give me true.

jsmtslch
  • 708
  • 7
  • 19
  • 1
    where you put the cron ? – John Joe Jul 12 '18 at 01:30
  • Let us say I have a method with parameters start time, end time and the cron expression, this method should tell me if the there was at least one instance when cron would have triggered in that time period. – jsmtslch Jul 12 '18 at 01:35
  • So you want a cron expression evaluator in Java? – Basil Bourque Jul 12 '18 at 02:43
  • Possibly a duplicate of others too, such as [this](https://stackoverflow.com/q/2362985/642706), [this](https://stackoverflow.com/q/5973797/642706), [this](https://stackoverflow.com/q/46344453/642706), and [this](https://stackoverflow.com/q/4363952/642706). Please search Stack Overflow thoroughly before posting. And explain how your Question is not addressed by past ones. – Basil Bourque Jul 12 '18 at 02:46
  • @BasilBourque Actually I was looking for a method which takes in start and end timestamp and a cron expression. Then it checks for how many times during this window the cron would have triggered. As of now, I cannot answer this question as this is marked duplicate so I am putting the logic in comments. – jsmtslch Jul 12 '18 at 19:30
  • public int compareCronWithDateRange(String cronExpn, Timestamp t1, Timestamp t2) { int counter = 0; if(cronExpn != null && !cronExpn.isEmpty()){ CronSequenceGenerator cronSeqGen = new CronSequenceGenerator(cronExpn); Date incrementalDate = t1; while(t2.after(incrementalDate)){ incrementalDate = cronSeqGen.next(incrementalDate); if(incrementalDate.before(t2)){ counter++; } } } return counter; } – jsmtslch Jul 12 '18 at 19:33
  • @BasilBourque - If this was a duplicate ask please let me know. I feel other question are asking something different. – jsmtslch Jul 12 '18 at 19:34
  • @BasilBourque - If you think this is not duplicate, please unmark it. Thanks! – jsmtslch Jul 16 '18 at 15:57

0 Answers0