fwq
2024-09-23 55334da20cff4dac840c320eda1d0ca53ba27eda
src/main/java/com/hx/util/DateUtil.java
@@ -1,5 +1,7 @@
package com.hx.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -452,6 +454,25 @@
    }
    /**
     * 获取两个时间相差分钟数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param remainder 余数进1
     * @return 分钟
     */
    public static int differMinute(Date startTime, Date endTime,boolean remainder) {
        BigDecimal sTime = new BigDecimal(startTime.getTime());
        BigDecimal eTime = new BigDecimal(endTime.getTime());
        eTime = eTime.subtract(sTime).setScale(0,RoundingMode.HALF_UP);
        if(remainder){
            eTime = eTime.divide(BigDecimal.valueOf(60000.0)).setScale(0, RoundingMode.UP);
        }else{
            eTime = eTime.divide(BigDecimal.valueOf(60000.0)).setScale(0, RoundingMode.DOWN);
        }
        return eTime.intValue();
    }
    /**
     * endTime比startTime多的天数
     * @param startTime 最小时间
     * @param endTime 最大时间
@@ -549,6 +570,7 @@
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }
@@ -572,9 +594,25 @@
    /**
     * 获取当前月的结束时间
     * @param date 点前时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     * @return 返回时间 格式yyyy-MM-dd 23:59:59999
     */
    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTime();
    }
    /**
     * 获取当前月的结束时间,没有毫秒
     * @param date 点前时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     */
    public static Date getMonthEndNoMillisecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
@@ -613,15 +651,33 @@
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }
    /**
     * 获取当前年的结束时间
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 00:00:00
     * @return 返回时间 格式yyyy-MM-dd 23:59:59999
     */
    public static Date getYearEnd(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTime();
    }
    /**
     * 获取当前年的结束时间,没有毫秒
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     */
    public static Date getYearEndNoMillisecond(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH, 11);
@@ -668,13 +724,27 @@
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    /**这天的最后时间
     * 日期2000-01-01变2000-01-01 23:59:59
     * 日期2000-01-01变2000-01-01 23:59:59999
     */
    public static Date dayToEndDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
    /**这天的最后时间,没有毫秒
     * 日期2000-01-01变2000-01-01 23:59:59
     */
    public static Date dayToEndDateNoMillisecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
@@ -780,4 +850,69 @@
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }
    /**
     * 根据出生年月日计算年龄
     * @param birth
     * @return
     */
    public static int getAge(Date birth) {
        Calendar cal = Calendar.getInstance();
        int thisYear = cal.get(Calendar.YEAR);
        int thisMonth = cal.get(Calendar.MONTH);
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birth);
        int birthYear = cal.get(Calendar.YEAR);
        int birthMonth = cal.get(Calendar.MONTH);
        int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        int age = thisYear - birthYear;
        // 未足月
        if (thisMonth <= birthMonth) {
            // 当月
            if (thisMonth == birthMonth) {
                // 未足日
                if (dayOfMonth < birthdayOfMonth) {
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }
    /**
     * 获取某天结束秒数
     * @param dateTime      日期
     * @param lateSecond    延迟秒数
     * @return
     */
    public static long todayEndSecond(Date dateTime, Long lateSecond) {
        if(dateTime == null){
            dateTime = new Date();
        }
        if(lateSecond == null){
            lateSecond = 0L;
        }
        Date endTime = DateUtil.dayToEndDate(dateTime);
        return differSecond(dateTime, endTime) + lateSecond;
    }
    /**
     * 计算2个实际相差秒数
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static long differSecond(Date startTime, Date endTime) {
        if(startTime == null || endTime == null){
            return 0L;
        }
        long sTime = startTime.getTime();
        long eTime = endTime.getTime();
        return (eTime - sTime) / 1000L;
    }
}