根据cron表达式输出匹配的n次执行时间

 只是为了自己用起来能快速找到,转自:https://www.cnblogs.com/wulm/p/6923545.html


	/**
     * 
     * @desc 计算表达式近n次时间
     * @auth josnow
     * @date 2019年2月23日 下午12:16:25
     * @param cron
     * @return
     */
    public static List seeExcuteTime(String cron) throws Exception {
        if (null==cron || "".equals(cron)) {
            throw new IllegalArgumentException("参数不能为空");
        }

        CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List list = new ArrayList<>(20);

        Date nextTimePoint = new Date();
        for (int i = 0; i < 500; i++) {
            // 计算下次时间点的开始时间
            nextTimePoint = cronSequenceGenerator.next(nextTimePoint);
            list.add(sdf.format(nextTimePoint));
        }
        return list;
    }

    public static void main(String[] args) throws Exception {
        List list = seeExcuteTime("0 0 0/1 4/1 * ?");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(i+1+"\t"+list.get(i));
        }
    }
    
    

 

 

你可能感兴趣的:(java)