k94314517
2025-05-20 e567a81f9dd15c15981c25576cc83df6331ea2b5
server/service/src/main/java/com/doumee/core/utils/DateUtil.java
@@ -1,11 +1,16 @@
package com.doumee.core.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
@@ -101,6 +106,33 @@
        try {
            m_intervalday = toDate.getTime() - fromDate.getTime();// 计算所得为微秒数
            m_intervalday = m_intervalday / 1000 / 60 / 60;// 计算所得的天数
            return m_intervalday + 1;
        } catch (Exception e) {
            return Integer.MIN_VALUE;
        }
    }
    public static long getBetweenMin(Date fromDate, Date toDate) {
        long m_intervalday = 0;// 初始化时间间隔的值为0
        // 使用的时间格式为yyyy-MM-dd
        try {
            m_intervalday = toDate.getTime() - fromDate.getTime();// 计算所得为微秒数
            m_intervalday = m_intervalday / 1000 / 60 ;// 计算所得的天数
            return m_intervalday + 1;
        } catch (Exception e) {
            return Integer.MIN_VALUE;
        }
    }
    public static long getBetweenDay(Date fromDate, Date toDate) {
        long m_intervalday = 0;// 初始化时间间隔的值为0
        // 使用的时间格式为yyyy-MM-dd
        try {
            m_intervalday = toDate.getTime() - fromDate.getTime();// 计算所得为微秒数
            m_intervalday = m_intervalday / 1000 / 60 / 60 / 24 ;// 计算所得的天数
            return m_intervalday + 1;
        } catch (Exception e) {
@@ -348,6 +380,8 @@
            throw e;
        }
    }
    /**
     * Descrption:取得当前日期,格式为:yyyy-MM-dd HH:mm:ss
@@ -882,95 +916,95 @@
     * @return 转换得到的日期
     */
    @SuppressWarnings("unchecked")
    public static Date stringToDate(String strDate, String oracleFormat) {
        if (strDate == null)
            return null;
        Hashtable<Integer, String> h = new Hashtable<Integer, String>();
        String javaFormat = new String();
        String s = oracleFormat.toLowerCase();
        if (s.indexOf("yyyy") != -1)
            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
        else if (s.indexOf("yy") != -1)
            h.put(new Integer(s.indexOf("yy")), "yy");
        if (s.indexOf("mm") != -1)
            h.put(new Integer(s.indexOf("mm")), "MM");
        if (s.indexOf("dd") != -1)
            h.put(new Integer(s.indexOf("dd")), "dd");
        if (s.indexOf("hh24") != -1)
            h.put(new Integer(s.indexOf("hh24")), "HH");
        if (s.indexOf("mi") != -1)
            h.put(new Integer(s.indexOf("mi")), "mm");
        if (s.indexOf("ss") != -1)
            h.put(new Integer(s.indexOf("ss")), "ss");
        int intStart = 0;
        while (s.indexOf("-", intStart) != -1) {
            intStart = s.indexOf("-", intStart);
            h.put(new Integer(intStart), "-");
            intStart++;
        }
        intStart = 0;
        while (s.indexOf("/", intStart) != -1) {
            intStart = s.indexOf("/", intStart);
            h.put(new Integer(intStart), "/");
            intStart++;
        }
        intStart = 0;
        while (s.indexOf(" ", intStart) != -1) {
            intStart = s.indexOf(" ", intStart);
            h.put(new Integer(intStart), " ");
            intStart++;
        }
        intStart = 0;
        while (s.indexOf(":", intStart) != -1) {
            intStart = s.indexOf(":", intStart);
            h.put(new Integer(intStart), ":");
            intStart++;
        }
        if (s.indexOf("年") != -1)
            h.put(new Integer(s.indexOf("年")), "年");
        if (s.indexOf("月") != -1)
            h.put(new Integer(s.indexOf("月")), "月");
        if (s.indexOf("日") != -1)
            h.put(new Integer(s.indexOf("日")), "日");
        if (s.indexOf("时") != -1)
            h.put(new Integer(s.indexOf("时")), "时");
        if (s.indexOf("分") != -1)
            h.put(new Integer(s.indexOf("分")), "分");
        if (s.indexOf("秒") != -1)
            h.put(new Integer(s.indexOf("秒")), "秒");
        int i = 0;
        while (h.size() != 0) {
            Enumeration e = h.keys();
            int n = 0;
            while (e.hasMoreElements()) {
                i = ((Integer) e.nextElement()).intValue();
                if (i >= n)
                    n = i;
            }
            String temp = (String) h.get(new Integer(n));
            h.remove(new Integer(n));
            javaFormat = temp + javaFormat;
        }
        SimpleDateFormat df = new SimpleDateFormat(javaFormat);
        Date myDate = new Date();
        try {
            myDate = df.parse(strDate);
        } catch (Exception e) {
            // e.printStackTrace();
            return null;
        }
        return myDate;
    }
//    public static Date stringToDate(String strDate, String oracleFormat) {
//        if (strDate == null)
//            return null;
//        Hashtable<Integer, String> h = new Hashtable<Integer, String>();
//        String javaFormat = new String();
//        String s = oracleFormat.toLowerCase();
//        if (s.indexOf("yyyy") != -1)
//            h.put(new Integer(s.indexOf("yyyy")), "yyyy");
//        else if (s.indexOf("yy") != -1)
//            h.put(new Integer(s.indexOf("yy")), "yy");
//        if (s.indexOf("mm") != -1)
//            h.put(new Integer(s.indexOf("mm")), "MM");
//
//        if (s.indexOf("dd") != -1)
//            h.put(new Integer(s.indexOf("dd")), "dd");
//        if (s.indexOf("hh24") != -1)
//            h.put(new Integer(s.indexOf("hh24")), "HH");
//        if (s.indexOf("mi") != -1)
//            h.put(new Integer(s.indexOf("mi")), "mm");
//        if (s.indexOf("ss") != -1)
//            h.put(new Integer(s.indexOf("ss")), "ss");
//
//        int intStart = 0;
//        while (s.indexOf("-", intStart) != -1) {
//            intStart = s.indexOf("-", intStart);
//            h.put(new Integer(intStart), "-");
//            intStart++;
//        }
//
//        intStart = 0;
//        while (s.indexOf("/", intStart) != -1) {
//            intStart = s.indexOf("/", intStart);
//            h.put(new Integer(intStart), "/");
//            intStart++;
//        }
//
//        intStart = 0;
//        while (s.indexOf(" ", intStart) != -1) {
//            intStart = s.indexOf(" ", intStart);
//            h.put(new Integer(intStart), " ");
//            intStart++;
//        }
//
//        intStart = 0;
//        while (s.indexOf(":", intStart) != -1) {
//            intStart = s.indexOf(":", intStart);
//            h.put(new Integer(intStart), ":");
//            intStart++;
//        }
//
//        if (s.indexOf("年") != -1)
//            h.put(new Integer(s.indexOf("年")), "年");
//        if (s.indexOf("月") != -1)
//            h.put(new Integer(s.indexOf("月")), "月");
//        if (s.indexOf("日") != -1)
//            h.put(new Integer(s.indexOf("日")), "日");
//        if (s.indexOf("时") != -1)
//            h.put(new Integer(s.indexOf("时")), "时");
//        if (s.indexOf("分") != -1)
//            h.put(new Integer(s.indexOf("分")), "分");
//        if (s.indexOf("秒") != -1)
//            h.put(new Integer(s.indexOf("秒")), "秒");
//
//        int i = 0;
//        while (h.size() != 0) {
//            Enumeration e = h.keys();
//            int n = 0;
//            while (e.hasMoreElements()) {
//                i = ((Integer) e.nextElement()).intValue();
//                if (i >= n)
//                    n = i;
//            }
//            String temp = (String) h.get(new Integer(n));
//            h.remove(new Integer(n));
//
//            javaFormat = temp + javaFormat;
//        }
//        SimpleDateFormat df = new SimpleDateFormat(javaFormat);
//
//        Date myDate = new Date();
//        try {
//            myDate = df.parse(strDate);
//        } catch (Exception e) {
//            // e.printStackTrace();
//            return null;
//        }
//
//        return myDate;
//    }
    public static Date StringToDate(String DATE1) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
@@ -1291,6 +1325,59 @@
        return days;
    }
    public static int calculateBetween(Date newDate, Date oldDate,Integer type){
        Calendar calo = Calendar.getInstance();
        Calendar caln = Calendar.getInstance();
        calo.setTime(oldDate);
        caln.setTime(newDate);
        if(type==0||type==1){
            return    calculateDaysBetweenDates(caln, calo)  + 1 ;
        }else if(type==2){
            int day = caln.get(Calendar.DATE);
            if(day == 1){
                return    calculateMonthsBetweenDates(caln, calo) + 1;
            }else{
                return    calculateMonthsBetweenDates(caln, calo);
            }
        }else if(type==3){
            return    calculateYearsBetweenDates(caln, calo) + 1;
        }
        return -1;
    }
    public static int calculateDaysBetweenDates(Calendar startDate, Calendar endDate) {
        long timeDiffInMillis = Math.abs(endDate.getTimeInMillis() - startDate.getTimeInMillis());
        return (int)(timeDiffInMillis / (24 * 60 * 60 * 1000));
    }
    public static int calculateMonthsBetweenDates(Calendar startDate, Calendar endDate) {
        int yearDiff = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
        int monthDiff = endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH);
        if (monthDiff < 0 || (monthDiff == 0 && endDate.get(Calendar.DAY_OF_MONTH) < startDate.get(Calendar.DAY_OF_MONTH))) {
            yearDiff--;
            monthDiff += 12;
        }
        return yearDiff * 12 + monthDiff;
    }
    public static int calculateYearsBetweenDates(Calendar startDate, Calendar endDate) {
        int yearDiff = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
        if (yearDiff > 0 && (startDate.get(Calendar.MONTH) >= endDate.get(Calendar.MONTH) ||
                (startDate.get(Calendar.MONTH) == endDate.get(Calendar.MONTH) && startDate.get(Calendar.DAY_OF_MONTH) <= endDate.get(Calendar.DAY_OF_MONTH)))) {
            yearDiff--;
        } else if (yearDiff < 0 && (startDate.get(Calendar.MONTH) <= endDate.get(Calendar.MONTH) ||
                (startDate.get(Calendar.MONTH) == endDate.get(Calendar.MONTH) && startDate.get(Calendar.DAY_OF_MONTH) >= endDate.get(Calendar.DAY_OF_MONTH)))) {
            yearDiff++;
        }
        return yearDiff;
    }
    /**
     * 取得与原日期相差一定天数的日期,返回Date型日期
@@ -2748,13 +2835,6 @@
        return sdfLongTimePlus.parse(sdfLongTimePlus.format(date));
    }
    public static void main(String[] args) {
        try {
            System.out.println(RandomStringUtils.randomNumeric(3));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
     *
@@ -2859,6 +2939,7 @@
        return nowDateString;
    }
    /**
     * @ author zhangyong @ 获取当月的第一天,2009-05-01
     */
@@ -2910,9 +2991,14 @@
        return w;
    }
    public static Long twoDaysBetween(Date beginDate, Date endDate) throws ParseException {
    public static Long twoDaysBetween(Date beginDate, Date endDate) {
        long minute = (endDate.getTime() - beginDate.getTime()) / (60 * 1000);
        return minute;
    }
    public static Integer daysBetweenDay(Date beginDate, Date endDate) {
        long day = (endDate.getTime() - beginDate.getTime()) / (60 * 1000 * 60 * 24);
        return Math.toIntExact(day);
    }
    /**
@@ -2943,6 +3029,21 @@
        return calendar.getTime();
    }
    /**
     * 获取指定日期的下个月第一天
     * @param date
     * @return
     */
    public static Date getNextMonthFirst(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1); // 将日历的月份增加1
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为下个月的第一天
        return calendar.getTime();
    }
    public static String afterDateToStr(Integer days){
        Date date = new Date();
@@ -2960,6 +3061,10 @@
    public static String getLongDate(Date date) {
        return DateToString(date, "yyyyMMddHHmmss");
    }
    public static String getLongDateTime(Date date) {
        return DateToString(date, "yyyy-MM-dd HH:mm:ss");
    }
    public static String DateToString(Date date, String dateStyle) {
@@ -3005,4 +3110,122 @@
        cal.add(Calendar.MINUTE,minutes);
        return cal.getTime();
    }
    /**
     * 获取某个时间X分钟后的时间
     * @param minutes
     * @param date
     * @return
     */
    public static Date afterMinutesByDate(Integer minutes,Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE,minutes);
        return cal.getTime();
    }
    public static Date afterDayByDate(Integer days,Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,days);
        return cal.getTime();
    }
    /**
     * 计算多少自然年/月/日后的日期
     * @param startDate 开始日期
     * @param type  0=日;1=月;2=年;
     * @param cycle 周期  X月
     * @return
     */
    public static Date afterDateByType(Date startDate,Integer type,Integer cycle){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        if(type==0){
            calendar.add(Calendar.DATE, cycle);
        }else if(type==1){
            calendar.add(Calendar.MONTH, cycle);
        }else if(type == 2) {
            calendar.add(Calendar.YEAR, cycle);
        }
        return calendar.getTime();
    }
    public static Integer monthDays(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获取当前年份、月份(注意月份从0开始计算)
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        // 根据年份和月份判断闰年情况
        boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
        // 根据不同月份设定每个月的天数
        switch (month) {
            case 2:
                if (isLeapYear) {
                    return 29;
                } else {
                    return 28;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                 return 30;
            default:
                 return 31;
        }
    }
    /**
     * 拼接日期数据
     * @param date
     * @param optType   1:每日开始 2:每日结束 3:昨日结束
     * @return
     */
    public static Date getMontageDate(Date date ,Integer optType) {
        String strShortDate = DateUtil.dateToString(date,"yyyy-MM-dd");
        if(optType.equals(1)){
            return DateUtil.StringToDate(strShortDate +" 00:00:00");
        }else if(optType.equals(2)){
            return DateUtil.StringToDate(strShortDate +" 23:59:59");
        }else if(optType.equals(3)){
            strShortDate = DateUtil.dateToString(DateUtil.getDateBetween(date,-1),"yyyy-MM-dd");
            return DateUtil.StringToDate(strShortDate +" 23:59:59");
        }else{
            return null;
        }
    }
    public static void main(String[] args) throws Exception{
        System.out.println(DateUtil.StringToDate("2025-05-16 13:49:40").getTime());
//        System.out.println(DateUtil.getLongDateTime(new Date()));;
    }
    /**
     * 获取相差的月数
     * @param sDate
     * @param eDate
     * @return
     */
    public static Integer getDifferenceMonths(Date sDate ,Date eDate) {
        LocalDate startDate = sDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
        LocalDate endDate = eDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
//        LocalDate startDate= LocalDate.of(2021,2,28);
//        LocalDate endDate =LocalDate.of(2022,4,5);
        Period period = Period.between(endDate,startDate);
        int months = period.getYears()* 12 + period.getMonths();
        return months + 1;
    }
}