-1

Is there any java libraries which I can use to convert cron expression into time interval? It could be Seconds, Milliseconds, Minutes etc...

Cron Expression (Input): 0/15 * * * * ?

Output1: 15 (getInSeconds)
Output2: 15000 (getInMilliSeconds)
Hemanth S R
  • 1,115
  • 2
  • 16
  • 27
  • http://stackoverflow.com/questions/4469276/convert-cron-expression-into-nice-description-strings-is-there-a-library-for-ja – assylias Jun 25 '15 at 14:57

2 Answers2

5

cron-utils does not give you intervals between executions (cron expressions may not have fix-sized time intervals. Example: if executing every first Sunday, time between executions will be contextual to months and years), but provides a method that returns last/next execution as well as Duration from last/next execution given a DateTime.

Here a snippet from the docs:

CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
CronParser parser = new CronParser(cronDefinition);

//Get date for last execution
DateTime now = DateTime.now();
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * * * *"));
DateTime lastExecution = executionTime.lastExecution(now));

//Get date for next execution
DateTime nextExecution = executionTime.nextExecution(now));

//Time from last execution
Duration timeFromLastExecution = executionTime.timeFromLastExecution(now);

//Time to next execution
Duration timeToNextExecution = executionTime.timeToNextExecution(now);
sashimi
  • 1,224
  • 2
  • 15
  • 23
-1

Take a look on Quartz. It not just parses the cron expressions but also runs tasks according to them. I believe you will find the way to extract interval using it.

AlexR
  • 114,158
  • 16
  • 130
  • 208