DateFormatUtil 时间格式化工具


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

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

/**
 * 日期格式化工具【<b>已添加常用格式类型,禁止修改,无特殊请款,不要随意添加</b>】
 * @author wysunmengyong
 * @description 年(y)、月(M)、日(d)、时(H)、分(m)、秒(s)、毫秒(S)
 * <br />SIMPLE,紧凑,不添加加分隔符,yyyyMMddHHmmss
 * <br />NORMAL,常用,添加常规分隔符,yyyy-MM-dd HH:mm:ss
 * @date 2015-11-7 下午3:18:35
 */
public class DateFormatUtil {
    /**SIMPLE: yyyyMMdd*/
    public static final String FORMAT_SIMPLE_DATE = "yyyyMMdd";
    /**NORMAL: yyyy-MM-dd*/
    public static final String FORMAT_NORMAL_DATE = "yyyy-MM-dd";
    /**SIMPLE: HHmmss*/
    public static final String FORMAT_SIMPLE_TIME = "HHmmss";
    /**NORMAL: HH:mm:ss*/
    public static final String FORMAT_NORMAL_TIME = "HH:mm:ss";
    /**SIMPLE: yyyyMMddHHmmss*/
    public static final String FORMAT_SIMPLE_DATE_TIME = "yyyyMMddHHmmss";
    /**NORMAL: yyyy-MM-dd HH:mm:ss*/
    public static final String FORMAT_NORMAL_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    /**SIMPLE: yyyyMMddHHmmssSSS*/
    public static final String FORMAT_SIMPLE_DATE_TIME_DETAIL = "yyyyMMddHHmmssSSS";
    /**NORMAL: yyyy-MM-dd HH:mm:ss.SSS*/
    public static final String FORMAT_NORMAL_DATE_TIME_DETAIL = "yyyy-MM-dd HH:mm:ss.SSS";
    
    /**
     * {@value #FORMAT_SIMPLE_DATE}
     * @author wysunmengyong
     * @description 
     * @date 2015-11-7 下午3:44:17
     * @param date
     * @return
     */
    public static String formatSimpleDate(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_SIMPLE_DATE);
    }
    
    /**
     * {@value #FORMAT_SIMPLE_DATE}
     * @author wysunmengyong
     * @description 
     * @date 2015-11-7 下午3:47:00
     * @param source 
     * @return
     * @throws ParseException
     */
    public static Date parseSimpleDate(String source) throws ParseException {
        return StringUtils.isBlank(source) ? null : new SimpleDateFormat(FORMAT_SIMPLE_DATE).parse(source);
    }

    /**
     * {@value #FORMAT_NORMAL_DATE}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午3:56:05
     * @param date
     * @return
     */
    public static String formatNormalDate(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_NORMAL_DATE);
    }

    /**
     * {@value #FORMAT_NORMAL_DATE}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:07:32
     * @param source
     * @return
     * @throws ParseException
     */
    public static Date parseNormalDate(String source) throws ParseException {
        return StringUtils.isBlank(source) ? null : new SimpleDateFormat(FORMAT_NORMAL_DATE).parse(source);
    }

    /**
     * {@value #FORMAT_SIMPLE_TIME}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:08:04
     * @param date
     * @return
     */
    public static String formatSimpleTime(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_SIMPLE_TIME);
    }

    /**
     * {@value #FORMAT_NORMAL_TIME}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:08:19
     * @param date
     * @return
     */
    public static String formatNormalTime(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_NORMAL_TIME);
    }

    /**
     * {@value #FORMAT_SIMPLE_DATE_TIME}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:08:34
     * @param date
     * @return
     */
    public static String formatSimpleDateTime(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_SIMPLE_DATE_TIME);
    }

    /**
     * {@value #FORMAT_NORMAL_DATE_TIME}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:08:47
     * @param date
     * @return
     */
    public static String formatNormalDateTime(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_NORMAL_DATE_TIME);
    }

    /**
     * {@value #FORMAT_NORMAL_DATE_TIME}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:09:13
     * @param source
     * @return
     * @throws ParseException
     */
    public static Date parseNormalDateTime(String source) throws ParseException {
        return StringUtils.isBlank(source) ? null : new SimpleDateFormat(FORMAT_NORMAL_DATE_TIME).parse(source);
    }

    /**
     * {@value #FORMAT_SIMPLE_DATE_TIME_DETAIL}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:10:47
     * @param date
     * @return
     */
    public static String formatSimpleDateTimeDetail(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_SIMPLE_DATE_TIME_DETAIL);
    }

    /**
     * {@value #FORMAT_NORMAL_DATE_TIME_DETAIL}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:09:45
     * @param date
     * @return
     */
    public static String formatNormalDateTimeDetail(Date date) {
        return (null == date) ? null : DateFormatUtils.format(date, FORMAT_NORMAL_DATE_TIME_DETAIL);
    }

    /**
     * 获取日期格式化对象
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:05:08
     * @param pattern 日期格式
     * @return
     */
    public static DateFormat getDateFormat(String pattern) {
        return new SimpleDateFormat(pattern);
    }

    /**
     * 通用日期格式化
     * @author wysunmengyong
     * @description 如果多次使用,建议直接创建格式化工具类【{@link #getDateFormat(String)}】
     * @date 2015-11-7 下午4:13:16
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        return getDateFormat(pattern).format(date);
    }

    /**
     * 通过格式化日期解析
     * @author wysunmengyong
     * @description 如果多次使用,建议直接创建格式化工具类【{@link #getDateFormat(String)}】
     * @date 2015-11-7 下午4:15:03
     * @param source
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date parse(String source, String pattern) throws ParseException {
        return getDateFormat(pattern).parse(source);
    }

    /**
     * {@value #FORMAT_SIMPLE_DATE_TIME}
     * @author wysunmengyong
     * @description
     * @date 2015-11-7 下午4:09:13
     * @param source
     * @return
     * @throws ParseException
     */
    public static Date parseSimpleDateTime(String source) throws ParseException {
        return StringUtils.isBlank(source) ? null : new SimpleDateFormat(FORMAT_SIMPLE_DATE_TIME).parse(source);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值