Skip to content Skip to sidebar Skip to footer

Cron Library For Java

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. CronExpressi

Solution 1:

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:0minutes:30hours:10,11,12,13daysOfMonth: ?
months: *
daysOfWeek:4,6lastdayOfWeek:falsenearestWeekday:falseNthDayOfWeek:0lastdayOfMonth:falseyears: *

Solution 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:

CronDefinitioncronDefinition= CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
CronParserparser=newCronParser(cronDefinition);

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

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

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

//Time to next executionDurationtimeToNextExecution= executionTime.timeToNextExecution(now);

Solution 3:

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 = newDate(); Date endDate = newDate(startDate.getTime() + 1000000);
    List<Date> dateList = TriggerUtils.computeFireTimesBetween(trigger, newBaseCalendar(), startDate, endDate);

    System.out.println("******Times**********");
    for(Date date : dateList) {
        System.out.println(date.toString());
    }
    System.out.println("*********************");

Solution 4:

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));

Post a Comment for "Cron Library For Java"