/cron/

 

QQ截图20140915153751

 

Java 解析Cron表达式,计算执行时间源码:

    @At
    @Ok("raw")
    public String getCron(@Param("exp") String exp,HttpServletResponse response) {
        response.setHeader("Content-Type", "text/javascript");//设置跨越访问 $.getScript()
        List<String> list = new ArrayList<String>();
        try {
            exp= URLDecoder.decode(Strings.sNull(exp),"utf-8");
            log.info(exp);
            CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
            cronTriggerImpl.setCronExpression(exp);
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();
            calendar.add(Calendar.MONTH, 1);//把统计的区间段
            List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, now, calendar.getTime());//这个是重点
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < dates.size(); i++) {
                if (i > 9) {//这个是提示的日期个数
                    break;
                }
                list.add(dateFormat.format(dates.get(i)));
            }
        } catch (Exception e) {
        }
        return "var data="+ Json.toJson(list);
    }

3,926 total views, 1 views today

One comment on “Cron:表达式在线验证工具

发表评论

电子邮件地址不会被公开。 必填项已用*标注