具体时间转换cron表达式

一个具体时间转换cron表达式

示例代码

package com.alex.callback;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    /**
     * 日期转化为cron表达式
     * @param date
     * @return
     */
    public static String getCron(java.util.Date  date){  
        String dateFormat="ss mm HH dd MM ? yyyy";  
        return  DateUtil.fmtDateToStr(date, dateFormat);  
    }  

    /**
     * cron表达式转为日期
     * @param cron
     * @return
     */
    public static Date getCronToDate(String cron) {  
        String dateFormat="ss mm HH dd MM ? yyyy";  
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);  
        Date date = null;  
        try {  
            date = sdf.parse(cron);  
        } catch (ParseException e) {  
            return null;
        }  
        return date;  
    }  

    /** 
     * Description:格式化日期,String字符串转化为Date 
     *  
     * @param date 
     * @param dtFormat 
     *            例如:yyyy-MM-dd HH:mm:ss yyyyMMdd 
     * @return 
     */  
    public static String fmtDateToStr(Date date, String dtFormat) {  
        if (date == null)  
            return "";  
        try {  
            SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);  
            return dateFormat.format(date);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return "";  
        }  
    } 
}

猜你喜欢

转载自blog.csdn.net/My_Way666/article/details/83823559