如何通过Cron表达式来获取最近运行时间?

by_封爱 版主 2016-05-27 02:31:45
quarzt.net大家都很熟悉了.代码也很简单.不过这个Cron表达式确实是比较费劲.

不过还好 由于网上有那种"表达式生成器"让我们节省了很多时间..比如下面的地址.

http://cron.qqe2.com/

随意百度出来的 都差不多是这个样子.不过这个网站比较特殊,他下面会根据你的表达式来返回你最近5次的运行时间.

比如 我设置了


0 55 23 1,11,21 * ?

每月的1号11号21号的23点55.

那么他在下面会返回


引用
最近5次运行时间:
2016/6/1 23:55:00
2016/6/11 23:55:00
2016/6/21 23:55:00
2016/7/1 23:55:00
2016/7/11 23:55:00



F12后发现他自己请求了自己的一个接口

http://cron.qqe2.com/CalcRunTime.ashx?CronExpression=0+55+23+1%2C11%2C21+*+%3F+

他这里的代码是如何实现的呢?

因为5次运行时间太少了 我想要10次..或者更多 因为我的表达式比较复杂,.5条记录还看不出来对与错...

...全文
1419 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
package com.xxl.job.admin.controller; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.quartz.TriggerUtils; import org.quartz.impl.triggers.CronTriggerImpl; import org.springframework.boot.SpringApplication; public class cronTest { public static void main(String[] args) throws Exception { cronTest cron= new cronTest(); //cron.getNextExecTime("0 0/1 * * * ? *", 3); System.out.println(cron.getNextExecTime("0 0 0 1 1,2 ?", 5)+"\n"); } public static List<String> getNextExecTime(String cronExpression,Integer numTimes) { List<String> list = new ArrayList<>(); CronTriggerImpl cronTriggerImpl = new CronTriggerImpl(); try { cronTriggerImpl.setCronExpression(cronExpression); } catch(ParseException e) { e.printStackTrace(); } // 这个是重点,一行代码搞定 List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, numTimes); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (Date date : dates) { list.add(dateFormat.format(date)); } return list; } }
  • 打赏
  • 举报
回复
我找到代码了
  • 打赏
  • 举报
回复
博主,请问你找到java运行最近5次的代码没有
ixiaoyang8 2017-05-27
  • 打赏
  • 举报
回复
你可以去这个网站上试一下,输入你的crontab表达式、开始执行的时间和执行的次数,计算出未来N次的时间

网址:http://www.matools.com/crontab

随风影随动 2017-05-04
  • 打赏
  • 举报
回复
public static String getCronSchdule(String cron){ String timeSchdule=""; if(!CronExpression.isValidExpression(cron)){ return "Cron is Illegal!"; } try { CronTrigger trigger = newTrigger().withIdentity("Caclulate Date").withSchedule(cronSchedule(cron)).build(); Date time0 = trigger.getStartTime(); Date time1 = trigger.getFireTimeAfter(time0); Date time2 = trigger.getFireTimeAfter(time1); Date time3 = trigger.getFireTimeAfter(time2); Date time4 = trigger.getFireTimeAfter(time3); Date time5 = trigger.getFireTimeAfter(time4); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); StringBuilder timeBuilder=new StringBuilder(); timeBuilder .append(format.format(time1)) .append("\n") .append(format.format(time2)) .append("\n") .append(format.format(time3)) .append("\n") .append(format.format(time4)) .append("\n") .append(format.format(time5)); timeSchdule=timeBuilder.toString(); } catch (Exception e) { timeSchdule="unKnow Time!"; } return timeSchdule; }
tanta 2016-05-30
  • 打赏
  • 举报
回复
没用过,帮顶
  • 打赏
  • 举报
回复
//
        // 摘要:
        //     Gets the next time to fire after the given time.
        //
        // 参数:
        //   afterTime:
        //     The time to compute from.
        protected DateTimeOffset? GetTimeAfter(DateTimeOffset afterTime);
        //
        // 摘要:
        //     NOT YET IMPLEMENTED: Returns the time before the given time that this Quartz.ICronTrigger
        //     will fire.
        //
        // 参数:
        //   date:
        //     The date.
        protected DateTimeOffset? GetTimeBefore(DateTimeOffset? date);
满满的都是现成的方法啊
  • 打赏
  • 举报
回复
Quartz.Impl.Triggers.CronTriggerImpl看起来可以直接用,如果不行的话,那就找下源代码,看内部是怎么处理的
public CronTriggerImpl(string name, string group, string cronExpression);
  • 打赏
  • 举报
回复
你是要反向解析Cron表达式咯? 这个按照quartz的官方说明自己反向写也可以,或者更直接点,下载quartz的源代码,找到那一部分的逻辑
Poopaye 2016-05-27
  • 打赏
  • 举报
回复
你说的是crontab?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧