5

I was wondering what is the most efficient way/best library to parse a cron expression and return a list of time points in Java.

For example I would have a cron expression, say, Fire every minute in October 2010 and would get a list/array of epoch times (or some other date format) returned that correspond to the times the trigger fires.

Thanks

jnovacho
  • 2,825
  • 6
  • 27
  • 44
noFearOfBeer
  • 77
  • 1
  • 7

2 Answers2

2

You could use org.quartz.CronExpression.getNextValidTimeAfter() . Using this method you can iteratively get as many trigger times as you wish.

You have to decide what will be the starting point of your iteration, will it be the current moment or epoch or smth else.

And you can parse a string cron expression into org.quartz.CronExpression using constructor CronExpression(String cronExpression).

EDIT: you can find a similar functionality in the Spring framework's CronSequenceGenerator. Both could be used in the similar iterative fashion so you could check which one suits you the best regarding performance etc.

S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • Thanks, actually thought about a solution like this when looking at the quartz library. Just having a massive iteration like this did not seem too efficient for many dates... – noFearOfBeer Mar 28 '15 at 15:38
  • This is the correct answer. @nofearofbeer's objection is odd. In order to construct a list out of a rule something will need to do a "massive iteration." A cron expression is not a list. – mttdbrd Mar 28 '15 at 17:05
  • Well, odd maybe. But I was referring to a "massive iteration like this", i.e. a long iteration over a method provided by a third party library. This typically creates some overhead, as a third party method requires a more general design (re-creating new instances of the return object could be one example). Just because a concept requires an iteration at some point does not mean that any type of iteration is equally efficient. – noFearOfBeer Mar 29 '15 at 07:45
  • @noFearOfBeer so did you mean in your original question only `most efficient` or `easy to use` as well? Cause I have understood by `best library` that it should be mature and easy to use. – S. Pauk Mar 29 '15 at 08:00
  • Hey @SergeyPauk, in my original question I wrote _I was wondering what is the most efficient way/best library to parse a cron expression_. So basically, I was interested in both aspects of the question. That said, I am not very familiar with this area, hence it might well be that your answer is the best solution for this if I don't want to re-invent the wheel. In my previous comment I was just sharing a potential downside I was seeing with this approach. But maybe there is no better solution if I don't want to re-code the entire thing myself. – noFearOfBeer Mar 29 '15 at 10:51
  • @noFearOfBeer You don't want to "parse" a cron expression. You want to generate a list of dates from a cron expression. You're looking for an efficient way to produce a list of dates iteratively. It sounds like you want a clever algorithm because producing a large list may be prohibitively expensive. So what you actually need to do? What are you trying to accomplish? – mttdbrd Mar 30 '15 at 03:11
2

cron-utils may be useful as an alternative to Quartz's CronExpression , since provides cron functionality without having to add a whole scheduler to the project. From the README, next execution can be calculated with the following snippet:

//Get date for next execution
DateTime now = DateTime.now();
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
CronParser parser = new CronParser(cronDefinition);
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * * * *"));
DateTime nextExecution = executionTime.nextExecution(now));

In the same README, cron-utils is described as

A Java library to parse, validate, migrate crons as well as get human readable descriptions for them. The project follows the Semantic Versioning Convention and uses Apache 2.0 license.

sashimi
  • 1,224
  • 2
  • 15
  • 23