问题描述
我有一个初始日期和一个cron表达式.我怎么能找到满足该cron表达式的下一个日期?.
I have an initial date and a cron expression. How could I find the next date satisfying this cron expression ?.
String cronExpresion = "* * * * * *"
LocalDateTime initial = LocalDateTime.now()
LocalDateTime next = ?
我尝试了CronSequenceGenerator及其 next()方法,但是它使用java.util.Date,我不希望不将LocalDateTime转换为Date,反之亦然.另外,我得到了几次不同的结果(两个日期之间的时间),如每10秒"这样的cron运行...
I tried CronSequenceGenerator and its next() method but it uses java.util.Date and I would prefer not to convert LocalDateTime to Date and vice versa. Plus, I've got different result (time between the 2 dates) from several run with a cron like 'every 10 secondes' ...
有什么主意吗?lib吗?
Any idea ? lib ?
推荐答案
欧姆尼克龙( https://github.com/intelie/omnicron )似乎至少满足了您的某些要求:
Omnicron (https://github.com/intelie/omnicron) seems to satisfy at least some of your requirements:
Cron cron = new Cron("*/5 * * * *");
ZonedDateTime date = ZonedDateTime.now();
System.out.println("Next execution: " + cron.next(date));
System.out.println("Prev execution: " + cron.prev(date));
(Cron :: next和Cron :: prev接受任何时间值,而不仅仅是ZonedDateTime.)
(Cron::next and Cron::prev accept any Temporal value, not just ZonedDateTime.)
除此之外,Omnicron还与Spring的CronSequenceGenerator( https://github.com/intelie/omnicron/blob/master/src/test/java/net/intelie/omnicron/SpringCompatibilityTest.java )
Aside from that, Omnicron is, as a bonus, compatible with Spring's CronSequenceGenerator (https://github.com/intelie/omnicron/blob/master/src/test/java/net/intelie/omnicron/SpringCompatibilityTest.java)
这篇关于如何从初始LocalDateTime和cron表达式中获取下一个LocalDateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!