4

I am looking for a cron expression library in java. Something that can parse cron expressions and return me future fire times for the trigger.

API on the lines of.

    CronExpression cronExpression = new CronExpression("0 30 4 * * *");
    List<Date> fireTimes = cronExpression.getFireTimes(todaysDate, nextWeekDate);

I don't want to use something as complicated as quartz. The purpose is to basically use cron like a regex for timings. That's all. I do not want a background scheduler.

I tried googling but wasn't able to find anything very helpful. Any suggestions would be appreciated.

Regards, Pulkit

P.S - I looked at using the CronExpression class out of quartz. Wasn't very helpful - failing some tests.

nutsiepully
  • 1,066
  • 3
  • 14
  • 21
  • Could you please post the test case that failed on CronExpression? Was that a limitation or bug? It would be helpful to all. – Ahamed Dec 17 '13 at 19:44
  • @Ahamed - using just the class out of quartz didn't work. look at my answer below as to how it worked finally. – nutsiepully Dec 20 '13 at 07:12

4 Answers4

3

You can definitely make use of cron4j for cron expessions and scheduling.

also you might find this post from chirag interesting,

cronTrigger.getExpressionSummary()
Example:

    CronTrigger t = new CronTrigger();
    t.setCronExpression("0 30 10-13 ? * WED,FRI");
    System.out.println(""+t.getExpressionSummary());
Output:

seconds: 0
minutes: 30
hours: 10,11,12,13
daysOfMonth: ?
months: *
daysOfWeek: 4,6
lastdayOfWeek: false
nearestWeekday: false
NthDayOfWeek: 0
lastdayOfMonth: false
years: *
Community
  • 1
  • 1
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
  • thanks for your reply. I know, but I don't want to schedule jobs. I just want to know what would the timings for a given cron expression be. – nutsiepully Nov 05 '13 at 03:47
2

Sounds like cron-utils may be useful to you. Is not a scheduler. Provides methods to handle a cron definition and return 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

I was able to solve the problem using dummy triggers on quartz. I didn't schedule and jobs etc, simply used the trigger api to compute all the times the job should fire based on a cron expression.

Best, Pulkit

    OperableTrigger trigger = (OperableTrigger)TriggerBuilder
            .newTrigger()
            .withIdentity("trigger1", "group1")
            .withSchedule(
                    SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(5).repeatForever()
            )
            .build();

    Date startDate = new Date(); Date endDate = new Date(startDate.getTime() + 1000000);
    List<Date> dateList = TriggerUtils.computeFireTimesBetween(trigger, new BaseCalendar(), startDate, endDate);

    System.out.println("******Times**********");
    for(Date date : dateList) {
        System.out.println(date.toString());
    }
    System.out.println("*********************");
nutsiepully
  • 1,066
  • 3
  • 14
  • 21
0

In case this is helpful to others too, I tried the other options but wasn't satisfied with any and ended up writing my own very small library just for that purpose, crony. It's available on maven-central.

The code you wanted would be, with crony:

Cron cronExpression = Cron.parseCronString("0 30 4 * * *").get();
Stream<ZonedDateTime> fireTimes = CronExecution
    .getNextExecutionDates(cron, todaysDate)
    .takeUntil(d -> d.isAfter(nextWeekDate));
Emmanuel Touzery
  • 9,008
  • 3
  • 65
  • 81