cron expression parsing into java date
Asked Answered
F

4

14
  • my database having 10 18 16 ? * SUN,MON,WED,FRI * cron expression then how to convert into Java date.
  • how to comparing with present day time.
  • and one more is how to compare to cron expressions i.e. 10 18 16 ? * SUN,MON,WED,FRI * and 0 30 9 30 * ?
  • please explain the sample code using quartz or spring scheduling.
Frescobaldi answered 23/5, 2014 at 17:27 Comment(0)
T
25

Please use:

import org.springframework.scheduling.support.CronSequenceGenerator;

final String cronExpression = "0 45 23 * * *";
final CronSequenceGenerator generator = new CronSequenceGenerator(cronExpression);
final Date nextExecutionDate = generator.next(new Date());

...and then I suggest use Joda DateTime for date comparison.

Tisman answered 15/12, 2015 at 13:22 Comment(4)
Excellent! Thank you very much!Rosena
Spring implementation of cron actionally lacks one feature - 'day' and 'day-of-week' fields are combined by AND logic, not by OR as in cron.Sergiosergipe
@Sergiosergipe i don't think tat this is a disadvantage... you can easily simulate "OR"-ed expressions by providing 2 expression. With AND you have the advantage that you can execute e.g. "every 2nd thuesday at..."Analysis
Deprecated: need to use CronExpression generator = CronExpression.parse(cronExpression)Allele
I
11

I wrote a small class for handling cron expressions, available here: https://github.com/frode-carlsen/cron

Based on Joda-time, but should be fairly easy to port to Java8 time api. This also makes it possible to embed in unit tests, do simulations etc by adjusting the DateTime offset in Joda-time.

It also has pretty good test coverage (was done as TDD Kata).

Update Now supports java 8 time api as well thanks to a contribution from github user https://github.com/zemiak. In both cases, the expression parser is a single, tiny class which can easily be copied into your own project.

Ing answered 30/5, 2014 at 8:46 Comment(5)
Is it on maven central?Fore
No, it's just a single class only depending on joda-time, so I haven't bothered. Feel free to fork or copyIng
Hey, this very cool. I've used your code to replace cron-utils which depend on guava, an over 2MB monster jar in my project: github.com/actframework/actframework/blob/master/src/main/java/…Burgoyne
Seems guava dependency is no longer required by cron-utils. Release 5.0.0 relies on JDK8 and no longer needs guava nor jodatime.Isothermal
This was exactly what I was looking for. A small class that does cron-like parsing. Thank you very much!Addams
M
7

You may want to look into the org.quartz.CronExpression class in the Quartz API.

Please note that you cannot simply compare a cron expression with a date because the cron expression (typically) represents a sequence of various dates. In any case, you may find the following methods useful:

public boolean isSatisfiedBy(Date date)
public Date getNextValidTimeAfter(Date date)

As for comparing two cron expressions, what would you like to compare? The only thing that IMO makes sense to compare are the next 'trigger' dates, i.e. dates obtained from getNextValidTimeAfter([some reference date]) calls.

Marhtamari answered 26/5, 2014 at 7:9 Comment(3)
Does it based on Joda-Time? or what?!Kolnos
We want to convert cron to java date. What is the method to achieve?Kolnos
You typically cannot convert a cron expression to a single Date instance because a cron expression usually represents a series of Dates. To obtain the the Date series, I think you can repeatedly call the getNextValidTimeAfter(Date) method and use the returned values as the input value for the next call. To answer your former question, Quartz API does not use Joda-Time, it uses java.util Date/Calendar APIs.Marhtamari
I
6

Perhaps you can check cron-utils It has some utils to get next/previous execution given certain date, ex.: now. Works with JodaTime, but you could retrieve a JavaDate from there. The library is scheduler agnostic: you just provide a string with a cron expression. Is compatible with JDK6.

Isothermal answered 20/5, 2015 at 16:2 Comment(5)
cron-utils is nice. The only thing is it has guava dependency which is over 2MBBurgoyne
Guava dependency was an issue. Since version 5.0.0 they removed it by migrating to JDK8. Now the library has even less dependencies, since they also got rid off jodatime leveraging new Java support for time.Isothermal
sounds good. But it now locked to Java8. I end up with using this single file parser: github.com/frode-carlsen/cron/blob/master/src/main/java/fc/cron/…Burgoyne
Sure, is an option :) Eventually you need to bind to some JDK.Isothermal
Thanks for this suggestion. I needed joda-time support in core-utils. Hence I choose the 4.1.3 version. Works as expected.Sternberg

© 2022 - 2024 — McMap. All rights reserved.