chengf
2025-07-22 b63f41b8a0cfce68770e5cb02c8dd3fd994f0c59
java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
@@ -5,6 +5,7 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;
/**
@@ -14,12 +15,13 @@
    private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
    public static final String LAST_TIME = "2038-01-01 00:00:00";
    public static final String LAST_TIME = "2050-01-01 00:00:00";
    private static Map<String, SimpleDateFormat> formats = new HashMap();
    public static final String DATE_FORMATE_STRING_DEFAULT = "yyyyMMddHHmmss";
    public static final String DATE_FORMATE_STRING_A = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMATE_STRING_B = "yyyy-MM-dd";
    public static final String DATE_FORMATE_STRING_SLASH_NON_ZERO = "yyyy/M/d";
    public static final String DATE_FORMATE_STRING_C = "MM/dd/yyyy HH:mm:ss a";
    public static final String DATE_FORMATE_STRING_D = "yyyy-MM-dd HH:mm:ss a";
    public static final String DATE_FORMATE_STRING_E = "yyyy-MM-dd'T'HH:mm:ss'Z'";
@@ -40,6 +42,7 @@
        formats.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
        formats.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        formats.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
        formats.put("yyyy/MM/dd", new SimpleDateFormat("yyyy/M/d"));
        formats.put("MM/dd/yyyy HH:mm:ss a", new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a"));
        formats.put("yyyy-MM-dd HH:mm:ss a", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"));
        formats.put("yyyy-MM-dd'T'HH:mm:ss'Z'", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
@@ -412,10 +415,10 @@
    public static Date getNextMonthFirstDate(Date curDate) {
        return getNextMonthFirstDate(curDate,1);
        return getNextMonthFirstDate(curDate, 1);
    }
    public static Date getNextMonthFirstDate(Date curDate,int monthCount) {
    public static Date getNextMonthFirstDate(Date curDate, int monthCount) {
        Calendar curDateCal = Calendar.getInstance();
        curDateCal.setTime(curDate);
@@ -817,6 +820,22 @@
        return calendar.getTime();
    }
    /**
     * 除去 小时 分 秒
     *
     * deadtime 本来就少了一秒
     *
     * @param time
     * @return
     */
    public static Date deadTimeToDate(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.SECOND,1);
        setTimeToMidnight(calendar);
        return calendar.getTime();
    }
    public static boolean sameMonthDay(Date startDate, Date endDate) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
@@ -824,7 +843,7 @@
        endCalender.setTime(endDate);
        if (
                startCalendar.get(Calendar.DAY_OF_MONTH) == endCalender.get(Calendar.DAY_OF_MONTH)
                && startCalendar.get(Calendar.HOUR_OF_DAY) == endCalender.get(Calendar.HOUR_OF_DAY)
                        && startCalendar.get(Calendar.HOUR_OF_DAY) == endCalender.get(Calendar.HOUR_OF_DAY)
        ) {
            return true;
        }
@@ -832,8 +851,8 @@
        return false;
    }
    public static double dayCompare(Date fromDate, Date toDate){
        return dayCompare(fromDate,toDate,false);
    public static double dayCompare(Date fromDate, Date toDate) {
        return dayCompare(fromDate, toDate, false);
    }
    /**
@@ -845,12 +864,12 @@
     * @param toDate
     * @return
     */
    public static double dayCompare(Date fromDate, Date toDate,boolean plusOneSec) {
    public static double dayCompare(Date fromDate, Date toDate, boolean plusOneSec) {
        if(plusOneSec){
        if (plusOneSec) {
            Calendar toD = Calendar.getInstance();
            toD.setTime(toDate);
            toD.add(Calendar.SECOND,1);
            toD.add(Calendar.SECOND, 1);
            toDate = toD.getTime();
        }
@@ -866,8 +885,8 @@
        Calendar fromDateCal = Calendar.getInstance();
        fromDateCal.setTime(fromDate);
        fromDateCal.set(Calendar.DAY_OF_MONTH, 1);
        fromDateCal.set(Calendar.HOUR_OF_DAY,0);
        fromDateCal.set(Calendar.MINUTE,0);
        fromDateCal.set(Calendar.HOUR_OF_DAY, 0);
        fromDateCal.set(Calendar.MINUTE, 0);
        if (fromDate.getTime() > fromDateCal.getTime().getTime()) {
            fromDateCal.add(Calendar.MONTH, 1);
            firstDay = false;
@@ -878,8 +897,8 @@
        Calendar toDateCal = Calendar.getInstance();
        toDateCal.setTime(toDate);
        toDateCal.set(Calendar.DAY_OF_MONTH, 1);
        toDateCal.set(Calendar.HOUR_OF_DAY,0);
        toDateCal.set(Calendar.MINUTE,0);
        toDateCal.set(Calendar.HOUR_OF_DAY, 0);
        toDateCal.set(Calendar.MINUTE, 0);
        if (toDate.getTime() > toDateCal.getTime().getTime()) {
            toDateFirstDate = toDateCal.getTime();
@@ -933,4 +952,68 @@
        endDate.add(Calendar.HOUR_OF_DAY, hour.intValue());
        return endDate.getTime();
    }
    public static String getNextSecTime(String time) {
        Date tTime = getDateFromStringA(time);
        return getNextSecTime(tTime);
    }
    public static Date getNextSecDateTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.SECOND, 1);
        return calendar.getTime();
    }
    public static String getNextSecTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.SECOND, 1);
        return getFormatTimeStringA(calendar.getTime());
    }
    public static String getPreSecTime(String time) {
        Date tTime = getDateFromStringA(time);
        return getPreSecTime(tTime);
    }
    public static String getPreSecTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.SECOND, -1);
        return getFormatTimeStringA(calendar.getTime());
    }
    public static Date getPreSecTime(Date time,int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MONTH,month);
        calendar.add(Calendar.SECOND, -1);
        return calendar.getTime();
    }
    public static String getPreSecTimeStr(Date time,int month) {
        return getFormatTimeStringB(getPreSecTime(time,month));
    }
    public static String calculateTimeDifference(Date start, Date end) {
        Duration duration = Duration.between(start.toInstant(), end.toInstant());
        long days = duration.toDays();
        long hours = duration.toHours() % 24;
        long minutes = duration.toMinutes() % 60;
        StringBuilder result = new StringBuilder();
        if (days > 0) {
            result.append(days).append("天");
        }
        if (hours > 0) {
            result.append(hours).append("时");
        }
        if (minutes > 0 || (days == 0 && hours == 0 && minutes == 0)) {
            result.append(minutes).append("分");
        }
        return result.toString();
    }
}