日期工具类

0. 全部

package com.wyc.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class DateUtil {

    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取今天的日期(默认格式)
     * @return
     */
    public static String getTodayDate(){
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
        return sdf.format(new Date());
    }

    /**
     * 获取今天的日期(指定格式)
     * @param format 日期格式
     * @return
     */
    public static String getTodayDate(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        return sdf.format(new Date());
    }

    /**
     * 获取当前时间(默认格式)
     * @return
     */
    public static String getCurrentTime(){
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
        return sdf.format(new Date());
    }

    /**
     * 获取今天的日期(指定格式)
     * @param format 日期格式
     * @return
     */
    public static String getCurrentTime(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        return sdf.format(new Date());
    }

    /**
     * 获取明天的日期
     * @param format  日期格式
     * @return
     */
    public static String getTomorrowDate(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取昨天的日期
     * @param format 日期格式
     * @return
     */
    public static String getYesterdayDate(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取几天后的日期
     * @param dateTime   日期
     * @param format  date日期以及返回的日期格式
     * @param day  后几天
     * @return
     */
    public static String getAfterDayDate(String dateTime, String format, int day){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(dateTime));
            calendar.add(Calendar.DATE,day);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            log.error("================获取几天后的日期出错,错误信息:{}",e);
        }
        return "";
    }

    /**
     * 获取几天前的日期
     * @param dateTime   日期
     * @param format  date日期以及返回的日期格式
     * @param day  前几天
     * @return
     */
    public static String getBeforeDayDate(String dateTime, String format, int day){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(dateTime));
            calendar.add(Calendar.DATE,-day);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            log.error("================获取几天前的日期出错,错误信息:{}",e);
        }
        return "";
    }

    /**
     * 转换日期格式
     * @param dateTime  时间
     * @param oldFormat 旧日期格式
     * @param newFormat 新日期格式
     * @return
     */
    public static String changeDateFormat(String dateTime,String oldFormat, String newFormat){
        SimpleDateFormat oldSdf = getSimpleDateFormat(oldFormat);
        SimpleDateFormat newSdf = getSimpleDateFormat(newFormat);
        try {
            Date date = oldSdf.parse(dateTime);
            return newSdf.format(date);
        } catch (ParseException e) {
            log.error("================转换日期格式出错,错误信息:{}",e);
        }
        return "";
    }

    /**
     * 将Date类型日期转换成字符串类型日期
     * @param date 时间
     * @param format 日期格式
     * @return
     */
    public static String changeDateToStr(Date date, String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 将字符串日期转换成Date类型日期
     * @param dateTime 时间
     * @param format 日期格式
     * @return
     */
    public static Date changeStrToDate(String dateTime, String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            return sdf.parse(dateTime);
        } catch (ParseException e) {
            log.error("================将字符串日期转换成Date类型日期出错,错误信息:{}",e);
        }
        return null;
    }

    /**
     * 根据出生日期获取年龄
     * @param birthday 出生日期
     * @param format 出生日期格式
     * @return
     */
    public static int getAgeByBirth(String birthday, String format){
        if (isEmpty(birthday)){
            return 0;
        }
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        int age = 0;
        try {
            Date birthDate = sdf.parse(birthday);
            Calendar cal = Calendar.getInstance();

            Calendar bir = Calendar.getInstance();
            bir.setTime(birthDate);
            // 如果生日大于当前日期,返回0
            if (cal.before(birthDate)){
                return 0;
            }
            // 取出当前的年月日
            int nowYear = cal.get(Calendar.YEAR);
            int nowMonth = cal.get(Calendar.MONTH);
            int nowDay = cal.get(Calendar.DAY_OF_MONTH);
            // 取出出生年月日
            int birYear = bir.get(Calendar.YEAR);
            int birMonth = bir.get(Calendar.MONTH);
            int birDay = bir.get(Calendar.DAY_OF_MONTH);
            // 大概年龄是当前年减去出生年
            age = nowYear - birYear;
            //如果出当前月小与出生月,或者当前月等于出生月但是当前日小于出生日,那么年龄age就减一岁
            if (nowMonth < birMonth || (nowMonth == birMonth && nowDay < birDay)){
                age--;
            }
        } catch (ParseException e) {
            log.error("================根据出生日期获取年龄出错,错误信息:{}",e);
        }
        return age;
    }

    /**
     * 获取两个时间相差多少天(日期格式yyyy-MM-dd)
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getDifferDay(String startTime, String endTime){
        if (isEmpty(startTime) || isEmpty(endTime)){
            return 0;
        }
        SimpleDateFormat sdf = getSimpleDateFormat(yyyy_MM_dd);
        try {
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            long result = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
            return Math.abs(result);
        } catch (ParseException e) {
            log.error("================获取两个日期相隔多少天出错,错误信息:{}",e);
        }
        return 0;
    }

    /**
     * 获取两个时间相差多少(返回的是毫秒)
     * @param startTime 开始时间
     * @param endTime  结束时间
     * @param format startTime和endTime的日期格式
     * @return
     */
    public static long getDifferTime(String startTime, String endTime, String format){
        if (isEmpty(startTime) || isEmpty(endTime)){
            return 0L;
        }
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            long differ = endDate.getTime() - startDate.getTime();
            return differ;
        } catch (ParseException e) {
            log.error("================获取两个日期相隔多长时间出错,错误信息:{}",e);
        }
        return 0L;
    }

    /**
     * 获取两个时间相差多少详细信息
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param format startTime和endTime的日期格式
     *               例如: 2021-04-01 00:00:01 和 2021-04-10 00:00:00  相差8天23小时59分59秒
     * @return
     */
    public static Map<String,Long> getDetailDifferTime(String startTime, String endTime, String format){
        Map<String, Long> map = new HashMap<>();
        long day = 0L;
        long hour = 0L;
        long minute = 0L;
        long seconds = 0L;
        if (!isEmpty(startTime) && !isEmpty(endTime)){
            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            long ns = 1000;

            long differ = getDifferTime(startTime, endTime, format);
            // 计算差多少天
            day = differ / nd;
            // 计算差多少小时
            hour = differ % nd / nh;
            // 计算差多少分钟
            minute = differ % nd % nh / nm;
            // 计算差多少秒
            seconds = differ % nd % nh % nm / ns;
        }
        map.put("day",day);
        map.put("hour",hour);
        map.put("minute",minute);
        map.put("seconds",seconds);
        return map;
    }

    /**
     * 判断两个日期的前后顺序
     * @param time1 日期1
     * @param time2 日期2
     * @param format 日期1和日期2的格式
     * @return true:time1在time2之后  false:time1在time2之前
     */
    public static boolean afterDate(String time1, String time2, String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Date date1 = sdf.parse(time1);
            Date date2 = sdf.parse(time2);
            return date1.after(date2);
        } catch (ParseException e) {
            log.error("================判断两个日期的前后顺序时间出错,错误信息:{}",e);
        }
        return false;
    }

    public static SimpleDateFormat getSimpleDateFormat(String format) {
        if (isEmpty(format)) {
            format = yyyy_MM_dd;
        }
        return new SimpleDateFormat(format);
    }

    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    private static boolean isEmpty(String str){
        if (str == null || "".equals(str.trim())){
            return true;
        }
        return false;
    }

}

1. 获取今天的日期

    /**
     * 获取今天的日期(默认格式)
     * @return
     */
    public static String getTodayDate(){
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
        return sdf.format(new Date());
    }

    /**
     * 获取今天的日期(指定格式)
     * @param format 日期格式
     * @return
     */
    public static String getTodayDate(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        return sdf.format(new Date());
    }

2. 获取当前时间

    /**
     * 获取当前时间(默认格式)
     * @return
     */
    public static String getCurrentTime(){
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
        return sdf.format(new Date());
    }

    /**
     * 获取当前的时间(指定格式)
     * @param format 日期格式
     * @return
     */
    public static String getCurrentTime(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        return sdf.format(new Date());
    }

3. 获取明天的日期

    /**
     * 获取明天的日期
     * @param format  日期格式
     * @return
     */
    public static String getTomorrowDate(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
        return sdf.format(calendar.getTime());
    }

4. 获取昨天的日期

    /**
     * 获取昨天的日期
     * @param format 日期格式
     * @return
     */
    public static String getYesterdayDate(String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return sdf.format(calendar.getTime());
    }

5. 获取几天后的日期

   /**
     * 获取几天后的日期
     * @param dateTime   日期
     * @param format  date日期以及返回的日期格式
     * @param day  后几天
     * @return
     */
    public static String getAfterDayDate(String dateTime, String format, int day){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(dateTime));
            calendar.add(Calendar.DATE,day);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            log.error("================获取几天后的日期出错,错误信息:{}",e);
        }
        return "";
    }

6. 获取几天前的日期

 /**
     * 获取几天前的日期
     * @param dateTime   日期
     * @param format  date日期以及返回的日期格式
     * @param day  前几天
     * @return
     */
    public static String getBeforeDayDate(String dateTime, String format, int day){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(dateTime));
            calendar.add(Calendar.DATE,-day);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            log.error("================获取几天前的日期出错,错误信息:{}",e);
        }
        return "";
    }

7. 转换日期格式

    /**
     * 转换日期格式
     * @param dateTime  时间
     * @param oldFormat 旧日期格式
     * @param newFormat 新日期格式
     * @return
     */
    public static String changeDateFormat(String dateTime,String oldFormat, String newFormat){
        SimpleDateFormat oldSdf = getSimpleDateFormat(oldFormat);
        SimpleDateFormat newSdf = getSimpleDateFormat(newFormat);
        try {
            Date date = oldSdf.parse(dateTime);
            return newSdf.format(date);
        } catch (ParseException e) {
            log.error("================转换日期格式出错,错误信息:{}",e);
        }
        return "";
    }

8. 将Date类型日期转换成字符串类型日期


    /**
     * 将Date类型日期转换成字符串类型日期
     * @param date 时间
     * @param format 日期格式
     * @return
     */
    public static String changeDateToStr(Date date, String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        return sdf.format(date);
    }

9. 将字符串日期转换成Date类型日期

    /**
     * 将字符串日期转换成Date类型日期
     * @param dateTime 时间
     * @param format 日期格式
     * @return
     */
    public static Date changeStrToDate(String dateTime, String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            return sdf.parse(dateTime);
        } catch (ParseException e) {
            log.error("================将字符串日期转换成Date类型日期出错,错误信息:{}",e);
        }
        return null;
    }

10. 根据出生日期获取年龄

    /**
     * 根据出生日期获取年龄
     * @param birthday 出生日期
     * @param format 出生日期格式
     * @return
     */
    public static int getAgeByBirth(String birthday, String format){
        if (isEmpty(birthday)){
            return 0;
        }
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        int age = 0;
        try {
            Date birthDate = sdf.parse(birthday);
            Calendar cal = Calendar.getInstance();

            Calendar bir = Calendar.getInstance();
            bir.setTime(birthDate);
            // 如果生日大于当前日期,返回0
            if (cal.before(birthDate)){
                return 0;
            }
            // 取出当前的年月日
            int nowYear = cal.get(Calendar.YEAR);
            int nowMonth = cal.get(Calendar.MONTH);
            int nowDay = cal.get(Calendar.DAY_OF_MONTH);
            // 取出出生年月日
            int birYear = bir.get(Calendar.YEAR);
            int birMonth = bir.get(Calendar.MONTH);
            int birDay = bir.get(Calendar.DAY_OF_MONTH);
            // 大概年龄是当前年减去出生年
            age = nowYear - birYear;
            //如果出当前月小与出生月,或者当前月等于出生月但是当前日小于出生日,那么年龄age就减一岁
            if (nowMonth < birMonth || (nowMonth == birMonth && nowDay < birDay)){
                age--;
            }
        } catch (ParseException e) {
            log.error("================根据出生日期获取年龄出错,错误信息:{}",e);
        }
        return age;
    }

11. 获取两个时间相差多少天(日期格式yyyy-MM-dd)

    /**
     * 获取两个时间相差多少天(日期格式yyyy-MM-dd)
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getDifferDay(String startTime, String endTime){
        if (isEmpty(startTime) || isEmpty(endTime)){
            return 0;
        }
        SimpleDateFormat sdf = getSimpleDateFormat(yyyy_MM_dd);
        try {
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            long result = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
            return Math.abs(result);
        } catch (ParseException e) {
            log.error("================获取两个日期相隔多少天出错,错误信息:{}",e);
        }
        return 0;
    }

12. 获取两个时间相差多少(返回的是毫秒)

    /**
     * 获取两个时间相差多少(返回的是毫秒)
     * @param startTime 开始时间
     * @param endTime  结束时间
     * @param format startTime和endTime的日期格式
     * @return
     */
    public static long getDifferTime(String startTime, String endTime, String format){
        if (isEmpty(startTime) || isEmpty(endTime)){
            return 0L;
        }
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            long differ = endDate.getTime() - startDate.getTime();
            return differ;
        } catch (ParseException e) {
            log.error("================获取两个日期相隔多长时间出错,错误信息:{}",e);
        }
        return 0L;
    }

13. 获取两个时间相差多少详细信息

    /**
     * 获取两个时间相差多少详细信息
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param format startTime和endTime的日期格式
     *               例如: 2021-04-01 00:00:01 和 2021-04-10 00:00:00  相差8天23小时59分59秒
     * @return
     */
    public static Map<String,Long> getDetailDifferTime(String startTime, String endTime, String format){
        Map<String, Long> map = new HashMap<>();
        long day = 0L;
        long hour = 0L;
        long minute = 0L;
        long seconds = 0L;
        if (!isEmpty(startTime) && !isEmpty(endTime)){
            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            long ns = 1000;

            long differ = getDifferTime(startTime, endTime, format);
            // 计算差多少天
            day = differ / nd;
            // 计算差多少小时
            hour = differ % nd / nh;
            // 计算差多少分钟
            minute = differ % nd % nh / nm;
            // 计算差多少秒
            seconds = differ % nd % nh % nm / ns;
        }
        map.put("day",day);
        map.put("hour",hour);
        map.put("minute",minute);
        map.put("seconds",seconds);
        return map;
    }

14. 判断两个日期的前后顺序

    /**
     * 判断两个日期的前后顺序
     * @param time1 日期1
     * @param time2 日期2
     * @param format 日期1和日期2的格式
     * @return true:time1在time2之后  false:time1在time2之前
     */
    public static boolean afterDate(String time1, String time2, String format){
        SimpleDateFormat sdf = getSimpleDateFormat(format);
        try {
            Date date1 = sdf.parse(time1);
            Date date2 = sdf.parse(time2);
            return date1.after(date2);
        } catch (ParseException e) {
            log.error("================判断两个日期的前后顺序时间出错,错误信息:{}",e);
        }
        return false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值