• cron表达式转换成时间(Java)


    cron时间表达式转换成时间

    有时候,为了使用者方便查看cron表达式的实际执行时间,我们需要将表达式转换成具体的时间,以显示给用户判断是否符合需求。

    cron表达式转换

    1. 引入依赖

    springboot项目引入依赖

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-quartzartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4

    maven项目引入依赖

    
    <dependency>
        <groupId>org.quartz-schedulergroupId>
        <artifactId>quartzartifactId>
        <version>2.3.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 编写代码
    package com.lcz.myquartz.util;
    
    import org.quartz.CronExpression;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    /**
     * 时间辅助类
     */
    public class DateUtils {
    
       /**
         * 验证cron表达式是否正确
         * @param cronExpression
         * @return
         */
        public static boolean isValid(String cronExpression){
            return CronExpression.isValidExpression(cronExpression);
        }
    
        /**
         * 将日期转换为cron时间
         *
         * @param date
         * @return
         */
        public static String parseCron(Date date) {
            String format="ss mm HH dd MM ? yyyy";
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(date);
        }
    
        /**
         * 获取下一次的执行时间
         *
         * @param cron
         * @return
         * @throws ParseException
         */
        public static Date nextTime(String cron, Date date) throws ParseException {
            // 加载包之后直接引用这个方法
            CronExpression cronExpression = new CronExpression(cron);
            // 转换 new Date 是为了给最近一次执行时间一个初始时间,这里给当前时间
            Date nextTime = cronExpression.getNextValidTimeAfter(date);
            return nextTime;
        }
    
        /**
         * 获取10个下次执行时间,如果不足10次,则返回实际次数
         * @param cron
         * @param date
         * @return
         * @throws ParseException
         */
        public static List<Date> next10Times(String cron, Date date) throws ParseException {
           return nextTimes(cron,date,10);
        }
    
        /**
         * 获取n个下次执行时间,如果不足n次,则返回实际次数
         * @param cron
         * @param date
         * @param n
         * @return
         * @throws ParseException
         */
        public static List<Date> nextTimes(String cron, Date date,int n) throws ParseException {
            List<Date> nextTimes = new ArrayList<>();
            Date nextTime = date;
            for (int i = 0; i < n; i++) {
                Date date1 = nextTime(cron, nextTime);
                if (null == date1) {
                    break;
                }
                nextTimes.add(date1);
                nextTime = date1;
            }
            return nextTimes;
        }
    
        /**
         * 时间转字符串
         * @param date
         * @return
         */
        public static String parseDate(Date date){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String dateFormat = sdf.format(date);
            return dateFormat;
        }
    
        /**
         * 字符串转时间
         * @param dateString
         * @return
         * @throws ParseException
         */
        public static Date toDate(String dateString) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date dateFormatParse = sdf.parse(dateString);
            return dateFormatParse;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    1. 测试用例,并运行
     @Test
        public void testNextTime() throws ParseException {
            String cron="0 0 1 * * ?";
            System.out.println("cron表达式:"+cron);
            System.out.println("--------获取下1次的执行时间-----------");
            //获取下一次的执行时间
            //下一次执行时间
            System.out.println(DateUtils.parseDate(DateUtils.nextTime(cron,new Date())));
    
            System.out.println("--------获取未来10次的执行时间-----------");
    
            //获取下10次执行时间
            List<Date> list= DateUtils.next10Times(cron,new Date());
            for(Date d:list){
                System.out.println(DateUtils.parseDate(d));
            }
    
            System.out.println("--------获取未来10次的执行时间-----------");
            String cronStr=DateUtils.parseCron(new Date(System.currentTimeMillis()+3600_000));
            System.out.println("1小时之后执行的cron表达式:"+cronStr);
            //获取下10次执行时间
            List<Date> list2= DateUtils.next10Times(cronStr,new Date());
            for(Date d:list2){
                System.out.println(DateUtils.parseDate(d));
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    API解析

    • DateUtils.parseCron(date)
      将时间转换成cron表达式,这个cron表达式表示date时间执行任务

    • DateUtils.nextTime(cronString,date)
      表示按照cron表达式,在date之后下一次的执行时间

    • DateUtils.next10Times(cronString,date)
      表示按照cron表达式,在date之后未来10次的执行时间(不足10次,则按照实际次数返回)

    • DateUtils.nextTimes(cronString,date,n)
      表示按照cron表达式,在date之后未来n次的执行时间(不足n次,则按照实际次数返回)

    传送门

    也可以使用网上提供的cron工具,在线解释cron表达式
    https://tool.lu/crontab/
    在这里插入图片描述

  • 相关阅读:
    Zabbix 5.0 使用自带Redis模版监控
    460.LFU缓存
    Spring事务传播
    卷不动了,还卷吗?
    分享画PAD图的软件-PADFlowChart
    sql语句数据库查询:如果当前元素已经使用过,下拉框不显示该元素该如何查询?
    Java面试——专业技能
    使用 VS Code + Markdown 编写 PDF 文档
    Android开发:gradle配置
    Ubuntu Zookeeper开机自启动服务
  • 原文地址:https://blog.csdn.net/u011628753/article/details/126492071