zoukankan      html  css  js  c++  java
  • Date与Quartz的cron之间的相互转换

    1.概述

    最近在写一个Quartz相关的模块,需要实现Date与Quartz的cron之间的相互转换,在网上查了一下竟然没有找到,但是找到一份这样的博客:http://hw1287789687.iteye.com/blog/2004202 给自己了启发,自己在参考该博主的基础上,增加了cron转date的方法,撰写该文章。

    2.实战

    直接上代码:

     
     
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    /**
     * 该类提供Quartz的cron表达式与Date之间的转换
     * Created by zhangzh on 2016/8/2.
     */
    public class CronDateUtils {
        private static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";
     
        /***
         *
         * @param date 时间
         * @return  cron类型的日期
         */
        public static String getCron(final Date  date){
            SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
            String formatTimeStr = "";
            if (date != null) {
                formatTimeStr = sdf.format(date);
            }
            return formatTimeStr;
        }
     
        /***
         *
         * @param cron Quartz cron的类型的日期
         * @return  Date日期
         */
     
        public static Date getDate(final String cron) {
     
     
            if(cron == null) {
                return null;
            }
     
            SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
            Date date = null;
            try {
                date = sdf.parse(cron);
            } catch (ParseException e) {
                return null;// 此处缺少异常处理,自己根据需要添加
            }
            return date;
        }
    }

    测试如下:

    public class Test {
      
        public static void main(String[] args) {
            Date now = new Date();
            System.out.println(CronDateUtils.getCron(now));
     
            String cron = "20 28 17 02 08 ? 2016";
     
            Date cronDate = CronDateUtils.getDate(cron);
            System.out.println("===================");
            System.out.println(cronDate.toString());
     
     
        }
    }

    输出:

    1.  
      30 15 16 05 08 ? 2016
    2.  
      ===================
    3.  
      Tue Aug 02 17:28:20 CST 2016

    转自:https://blog.csdn.net/zhglance/article/details/52130131

  • 相关阅读:
    Excel与Google Sheets中实现线性规划求解
    Optaplanner终于支持多线程并行运行
    【译】Optaplanner开发手册本地化: (0)
    多工序、多机台(产线)环境下的排程要点
    设计Optaplanner下实时规划服务的失败经历
    Optaplanner规划引擎的工作原理及简单示例(2)
    vim 分屏
    vim autocmd
    irun vcs option
    记录
  • 原文地址:https://www.cnblogs.com/azhqiang/p/9318646.html
Copyright © 2011-2022 走看看