Java 日期cron表达式 转换

xiaoxiao2021-02-28  13

spring quartz 日期/cron表达式 转换


需求用户自定义时间推送文章,实现使用了spring quartz 做了一个任务管理模块 。下面代码是日期/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 ""; } } }
转载请注明原文地址: https://www.6miu.com/read-800119.html

最新回复(0)