DateUtils工具类

下面这个是自己封装的DateUtil的工具类,主要是觉得有一些方法比较实用 封装了一下,有用到的可以直接复制一下代码。。。

package com.xindong.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 例子 for example
 * <p>
 * Date startTime = DateUtils.getStartTime(new Date());                 // Thu May 21 00:00:00 CST 2020
 * <p>
 * String str = DateUtils.dateToStr(startTime, DateUtils.COMMON_DATE);  // 2020年05月21日 00时00分00秒
 * <p>
 * Date date = DateUtils.stringToDate(str, DateUtils.COMMON_DATE);      // Thu May 21 00:00:00 CST 2020
 */

@Service
@Slf4j
public class DateUtils {
    /**
     * 默认的日期类型
     * EEE MMM dd HH:mm:ss zzz yyyy
     */
    public static final String DEFAULT_DATE = "EEE MMM dd HH:mm:ss zzz yyyy";
    /**
     * 一般日期格式
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String GENERAL_DATE = "yyyy-MM-dd HH:mm:ss";
    /**
     * 常见日期格式
     * yyyy年MM月dd日 HH时mm分ss秒
     */
    public static final String COMMON_DATE = "yyyy年MM月dd日 HH时mm分ss秒";


    /**
     * 将字符串转化为对应的日期格式
     * (若是不对应的话会报错)
     *
     * @param dateStr
     * @param formatStr 需要转换的时间格式
     * @return
     * @throws Exception
     */
    public static Date stringToDate(String dateStr, String formatStr) throws Exception {
        Date date = new Date();
        try {
            if (StringUtils.isEmpty(dateStr)) {
                return null;
            }
            if (StringUtils.isEmpty(formatStr)) {
                formatStr = DateUtils.GENERAL_DATE;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            log.error(String.format("字符串转换指定日期错误--错误的字符串为 %s --异常为 %s",
                    dateStr, e.getMessage()));
            throw e;
        }
        return date;
    }

    /**
     * 将日期转化为相对应的字符串
     *
     * @param date
     * @param formatStr 需要转换的时间格式
     * @return
     * @throws Exception
     */
    public static String dateToStr(Date date, String formatStr) throws Exception {
        String dateStr = null;
        try {
            if (date == null) {
                return null;
            }
            //若是转换的格式为空的话,以此作为默认日期格式
            if (formatStr == null) {
                formatStr = DateUtils.GENERAL_DATE;
            }
//            SimpleDateFormat sdf = null;
//            if (isEnglish == null ? false : isEnglish) {
//                sdf = new SimpleDateFormat(formatStr, Locale.ENGLISH);
//            } else {
//                sdf = new SimpleDateFormat(formatStr);
//            }
            SimpleDateFormat sdf = new SimpleDateFormat(formatStr);

            dateStr = sdf.format(date);
        } catch (Exception e) {
            log.error(String.format("日期转化字符串错误--错误的日期为 %s --异常为 %s",
                    date, e.getMessage()));
            throw e;
        }
        return dateStr;
    }

    /**
     * 取得当前日期字符串
     *
     * @return 格式:scheme 按照自己想得到的格式
     */
    public static String getCurrDate(String scheme) {
        return DateFormatUtils.format(new Date(), scheme);
    }

    /**
     * 获取当天00:00:00的时间
     *
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取当天23:59:59 的时间
     *
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 取得离现在n天的时间字符串
     *
     * @param afterDay 可往前(负数)、往后(正数)n天
     * @return 格式:yyyy-MM-dd HH:mm:ss
     */
    public static String getAfterTime(int afterDay) throws Exception {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, afterDay);
        return dateToStr(calendar.getTime(), null);
    }

    /**
     * 比较date1是否早于date2
     *
     * @param date1
     * @param date2
     * @return
     * @throws Exception
     */
    public static boolean compareDate(Date date1, Date date2) throws Exception {
        if (date1 == null || date2 == null) {
            log.error(String.format("填入的日期不能为空"));
            throw new Exception("填入的日期不能为空");
        }
        return date1.before(date2);
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断输入年份是否为闰年
     *
     * @param year 年份
     * @return
     */
    public static boolean leapYear(int year) {

        return year % 4 == 0 ? year % 100 == 0 ? year % 400 == 0 ? true : false : true : false;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值