Java Date常用工具类


本文章为自己梳理的一些常用DateUtil工具类,不定期修复BUG并进行代码整理

1.常量

	/** 天时间戳 */
    public static final Integer DAY_TIME_STAMP = 86400000;
    /** 小时时间戳 */
    public static final Integer HOUR_TIME_STAMP = 3600000;
    /** 分钟时间戳 */
    public static final Integer MINUTE_TIME_STAMP = 60000;
    /** 秒时间戳 */
    public static final Integer SECOND_TIME_STAMP = 1000;
    /** 秒 */
    public static final String SECOND = "second";
    /** 分 */
    public static final String MINUTE = "minute";
    /** 小时 */
    public static final String HOUR = "hour";
    /** 天 */
    public static final String DAY = "day";
    /** 星期一 */
    public static final String MON = "mon";
    /** 星期二 */
    public static final String TUES = "tues";
    /** 星期三 */
    public static final String WED = "wed";
    /** 星期四 */
    public static final String THUR = "thur";
    /** 星期五 */
    public static final String FRI = "fri";
    /** 星期六 */
    public static final String SAT = "sat";
    /** 星期日 */
    public static final String SUN = "sun";
    /**
     * =========================== 日期格式 ===============================
     */
    public static final String DATE_YYYYHHDD = "yyyyMMdd";
    public static final String DATE_HHMM = "HHmm";
    public static final String DATE_YYYY_HH_DD = "yyyy-MM-dd";
    public static final String DATE_YYYY_HH_DD_INCLINED = "yyyy/MM/dd";
    public static final String DATE_HH_MM = "HH:mm";
    /**
     * ========================== 日期时间格式 ===============================
     */
    public static final String DATE_YYYYHHDD_HHMMSS = "yyyyMMddHHmmss";
    public static final String PATTERN_DEFAULT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_YYYYHHDD_HHMM = "yyyyMMddHHmm";

2.传入日期时间,随后根据指定的类型返回

/**
     * @param dateObj    日期时间数据
     *                   Long = 时间戳
     *                   String = 日期字符串
     *                   Date = 日期时间类型
     * @param dateType   返回与识别 日期时间格式 例:yyyy-MM-dd HH:mm:ss
     * @param returnType 返回的数据类型
     * @return Object
     * @Description 传入指定的类型进行识别日期时间,随后根据指定的类型返回数据
     * @Date 13:44 2022/11/3
     **/
    public static Object getDateByType(Object dateObj, String dateType, Object returnType) {
    
    
        Date date;
        Long timestamp;
        if (ObjectUtils.isEmpty(dateType)) {
    
    
            dateType = PATTERN_DEFAULT_DATETIME;
        }
        SimpleDateFormat receiveDateFormat = new SimpleDateFormat(dateType);
        if (dateObj instanceof Long) {
    
    
            timestamp = (Long) dateObj;
        } else if (dateObj instanceof String) {
    
    
            try {
    
    
                String dateStr = dateObj.toString();
                date = receiveDateFormat.parse(dateStr);
                timestamp = date.getTime();
            } catch (ParseException e) {
    
    
                e.printStackTrace();
                return null;
            }
        } else if (dateObj instanceof Date) {
    
    
            date = (Date) dateObj;
            timestamp = date.getTime();
        } else {
    
    
            return null;
        }
        date = new Date(timestamp);
        if (Long.class.equals(returnType)) {
    
    
            return date.getTime();
        } else if (String.class.equals(returnType)) {
    
    
            return receiveDateFormat.format(date);
        } else if (Date.class.equals(returnType)) {
    
    
            return date;
        } else {
    
    
            return null;
        }
    }

    /**
     * @param dateObj        日期时间数据
     *                       Long = 时间戳
     *                       String = 日期字符串
     *                       Date = 日期时间类型
     * @param dateType       返回与识别 日期时间格式 例:yyyy-MM-dd HH:mm:ss
     * @param returnDateType 返回的
     * @return Object
     * @Description 传入指定的类型进行识别日期时间,随后根据指定的类型返回数据
     * @Date 13:44 2022/11/3
     **/
    public static String getDateByType(Object dateObj, String dateType, String returnDateType) {
    
    
        Long dateLong = (Long) getDateByType(dateObj, dateType, Long.class);
        return (String) getDateByType(dateLong, returnDateType, String.class);
    }

3.获取前N小时日期时间 || 后N小时时间

/**
     * @param dateObj     Long = 时间戳
     *                    String = 日期字符串
     *                    Date = 日期时间类型
     * @param dateType    返回与识别 日期时间格式 例:yyyy-MM-dd HH:mm:ss
     * @param timeObj     时间数
     * @param dateType    时间数 是 秒? 分钟? 小时?
     * @param isFrontBack 是否前推
     * @param returnType  返回的数据类型
     * @return Object
     * 默认 yyyy-MM-dd HH:mm:ss (若 dateType 参数不为空则返回该格式)
     * @Description 获取前N小时日期时间  || 后N小时时间
     * @Date 13:48 2022/11/2
     **/
    public static Object getBeforeByHourTime(Object dateObj, String dateType, String timeType, Object timeObj, boolean isFrontBack, Object returnType) {
    
    
        Long hour;
        if (timeObj instanceof Long) {
    
    
            hour = (Long) timeObj;
        } else if (timeObj instanceof Integer) {
    
    
            hour = Long.valueOf((Integer) timeObj);
        } else {
    
    
            return null;
        }
        Long dateLong = (Long) getDateByType(dateObj, dateType, Long.class);
        Long result;
        if (timeType.equals(HOUR)) {
    
    
            hour *= HOUR_TIME_STAMP;
        } else if (timeType.equals(MINUTE)) {
    
    
            hour *= MINUTE_TIME_STAMP;
        }
        if (isFrontBack) {
    
    
            result = dateLong - hour;
        } else {
    
    
            result = dateLong + hour;
        }
        Date date = new Date(result);
        if (Long.class.equals(returnType)) {
    
    
            return date.getTime();
        } else if (String.class.equals(returnType)) {
    
    
            if (ObjectUtils.isEmpty(dateType)) {
    
    
                dateType = PATTERN_DEFAULT_DATETIME;
            }
            SimpleDateFormat receiveDateFormat = new SimpleDateFormat(dateType);
            return receiveDateFormat.format(date);
        } else if (Date.class.equals(returnType)) {
    
    
            return date;
        } else {
    
    
            return null;
        }
    }

4.计算两个时间差

	/**
     * @param startDate 开始时间
     *                  Long = 时间戳
     *                  Date = 日期时间类型
     * @param endDate   结束时间
     *                  Long = 时间戳
     *                  Date = 日期时间类型
     * @param getType   要获取 秒差? 分钟差? 小时差?
     * @return Long
     * @Description 计算两个时间差
     * @Date 14:36 2022/11/3
     **/
    public static Long getDatePoor(Object startDate, Object endDate, String getType) {
    
    
        Long startDateLong = (Long) getDateByType(startDate, null, Long.class);
        Long endDateLong = (Long) getDateByType(endDate, null, Long.class);
        long diff;
        if (startDateLong > endDateLong) {
    
    
            diff = startDateLong - endDateLong;
        } else {
    
    
            diff = endDateLong - startDateLong;
        }
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        if (SECOND.equals(getType)) {
    
    
            return diff / ns;
        } else if (MINUTE.equals(getType)) {
    
    
            return diff / nm;
        } else if (HOUR.equals(getType)) {
    
    
            return diff / nh;
        } else if (DAY.equals(getType)) {
    
    
            return diff / nd;
        } else {
    
    
            return null;
        }
    }

    /**
     * @param startDate 开始时间
     *                  Long = 时间戳
     *                  String = 日期字符串
     *                  Date = 日期时间类型
     * @param endDate   结束时间
     *                  Long = 时间戳
     *                  String = 日期字符串
     *                  Date = 日期时间类型
     * @param getType   要获取 秒差? 分钟差? 小时差?
     * @param dateType  两个日期的字符串格式 例:yyyy-MM-dd HH:mm:ss
     * @return Long
     * @Description 计算两个时间差
     * @Date 14:36 2022/11/3
     **/
    public static Long getDatePoor(Object startDate, Object endDate, String getType, String dateType) {
    
    
        Object startDateLong;
        if (startDate instanceof String) {
    
    
            startDateLong = getDateByType(startDate, dateType, Long.class);
        } else {
    
    
            startDateLong = startDate;
        }
        Object endDateLong;
        if (endDate instanceof String) {
    
    
            endDateLong = getDateByType(endDate, dateType, Long.class);
        } else {
    
    
            endDateLong = endDate;
        }
        return getDatePoor(startDateLong, endDateLong, getType);
    }

    /**
     * @param startDate     开始时间
     * @param endDate       结束时间
     * @param getType       要获取 秒差? 分钟差? 小时差?
     * @param startDateType 开始日期的字符串格式 例:yyyy-MM-dd HH:mm:ss
     * @param endDateType   结束日期的字符串格式 例:yyyy-MM-dd HH:mm:ss
     * @return Long
     * @Description 计算两个时间差
     * @Date 14:36 2022/11/3
     **/
    public static Long getDatePoor(String startDate, String endDate, String getType, String startDateType, String endDateType) {
    
    
        Object startDateLong = getDateByType(startDate, startDateType, Long.class);
        Object endDateLong = getDateByType(endDate, endDateType, Long.class);
        return getDatePoor(startDateLong, endDateLong, getType);
    }

5.获取日期是周几

	/**
     * @param dateObj  日期
     * @param dateType 字符串日期格式
     * @return String
     * @Description 获取今天是周几
     * @Date 17:17 2022/11/3
     **/
    public static String getWeekOfDate(Object dateObj, String dateType) {
    
    
        Date date = (Date) getDateByType(dateObj, dateType, Date.class);
        String[] weekDays = {
    
    SUN, MON, TUES, WED, THUR, FRI, SAT};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        w = w < 0 ? 0 : w;
        return weekDays[w];
    }

6.获取下个月第一天

	/**
     * @param dateStr 日期数据
     * @param format  日期格式
     * @return String
     * @Description 获取下个月第一天
     * @Date 10:49 2022/12/12
     **/
    public static String getFirstDayOfNextMonth(String dateStr, String format) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
    
    
            Date date = sdf.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.MONTH, 1);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        return null;
    }

7.使用已知的格式判断是否为日期格式

	/**
     * @Description 使用已知的格式判断是否为日期格式,是则返回 Date,否则返回Null
     * @Date 11:19 2023/2/9
     * @param dateTime 日期String格式
     **/
    public static Date getDateByUnknownType(String dateTime) {
    
    
        Date date = (Date) getDateByType(dateTime, DATE_YYYY_HH_DD, Date.class);
        if (!ObjectUtils.isEmpty(date)) {
    
    
            return date;
        }
        date = (Date) getDateByType(dateTime, DATE_YYYYHHDD, Date.class);
        if (!ObjectUtils.isEmpty(date)) {
    
    
            return date;
        }
        date = (Date) getDateByType(dateTime, DATE_YYYY_HH_DD_INCLINED, Date.class);
        if (!ObjectUtils.isEmpty(date)) {
    
    
            return date;
        }
        return null;
    }

猜你喜欢

转载自blog.csdn.net/c15112120076/article/details/129039017