jiangping
2024-10-21 291baf8ad5bd225c426b5cbfff38168ffda251cd
server/system_service/src/main/java/com/doumee/core/utils/DateUtil.java
@@ -317,6 +317,53 @@
        }
    }
    /**
     * 获取X月份之前的月份数
     * @param currentDate
     * @param month
     * @return
     */
    public static List<String> getBeforMonth(Date currentDate , Integer month) {
        SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
        int currentYear = Integer.parseInt(sdfYear.format(currentDate));
        int currentMonth = Integer.parseInt(sdfMonth.format(currentDate));
        List<String> list = new ArrayList<>();
        for (int i = 0; i < month; i++) {
            int tempYear = currentYear;
            int tempMonth = currentMonth - i;
            if (tempMonth <= 0) {
                tempYear--;
                tempMonth += 12;
            }
            list.add(tempYear +"-" + StringUtils.leftPad(Integer.toString(tempMonth),2,"0"));
        }
        return list;
    }
    public static List<String> getBeforDays(Date currentDate,Integer days){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        Date endDate = currentDate; // 获取当前日期
        calendar.add(Calendar.DAY_OF_YEAR, -days); // 往前推7天
        Date startDate = calendar.getTime(); // 获取推算后的日期
        List<String> list = new ArrayList<>();
        while (startDate.before(endDate)) {
            calendar.add(Calendar.DAY_OF_YEAR, 1); // 获取下一天日期
            startDate = calendar.getTime();
            list.add(DateUtil.dateToString(startDate,"yyyy-MM-dd"));
            System.out.println(DateUtil.dateToString(startDate,"yyyy-MM-dd"));
        }
        return list;
    }
    /**
     * Descrption:取得当前日期时间,格式为:YYYYMMDDHHMISS
     *
@@ -1229,6 +1276,20 @@
    }
    /**
     * 得到X分钟后的时间(时间格式)
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date getXMinuteAfterDate(Date date, int minute) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minute);
        return now.getTime();
    }
    /**
     * 得到两个日期之间相差的天数
     *
     * @param newDate
@@ -1823,14 +1884,6 @@
    // StringUtil.getNotNullStr(request.getParameter(strParamName));
    // return getDateByString(strStr);
    // }
    /**
     * 得到当前日期,格式yyyy-MM-dd。
     *
     * @return String 格式化的日期字符串
     */
    public static String getCurrDate() {
        return getFormattedDate(getDateByString(""));
    }
    /**
     * 得到当前日期,格式yyyy-MM-dd。
@@ -1893,6 +1946,17 @@
    public static String getCurrDateTime() {
        Timestamp date = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return formatter.format(date);
    }
    /**
     * 得到当前日期时间,格式为yyyy-MM-dd.
     *
     * @return String
     */
    public static String getCurrDate() {
        Timestamp date = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }
@@ -2758,16 +2822,18 @@
//            System.out.println(DateUtil.afterMinutesDate(-5));
            Date startTime = DateUtil.StringToDate("2024-05-01 08:00:00" ,"yyyy-MM-dd HH:mm:ss");
            Date endTime = DateUtil.StringToDate("2024-05-01 17:00:00" ,"yyyy-MM-dd HH:mm:ss");
            Long intervalTime = 35L;
            DateUtil.getBeforDays(new Date(),7);
            DateUtil.checkDateInterval( startTime.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime(), endTime.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime(),intervalTime);
//            Date startTime = DateUtil.StringToDate("2024-05-01 08:00:00" ,"yyyy-MM-dd HH:mm:ss");
//            Date endTime = DateUtil.StringToDate("2024-05-01 17:00:00" ,"yyyy-MM-dd HH:mm:ss");
//            Long intervalTime = 35L;
//
//
//            DateUtil.checkDateInterval( startTime.toInstant()
//                    .atZone(ZoneId.systemDefault())
//                    .toLocalDateTime(), endTime.toInstant()
//                    .atZone(ZoneId.systemDefault())
//                    .toLocalDateTime(),intervalTime);
        } catch (Exception ex) {
            ex.printStackTrace();
@@ -2964,6 +3030,13 @@
    public static String afterDateToStr(Integer days){
        Date date = new Date();
        Long l = date.getTime()+1000*60*60*24*days;
        date.setTime(l);
        return DateToStr(date, "yyyyMMdd");
    }
    public static String afterDateHourToStr(Integer hour){
        Date date = new Date();
        Long l = date.getTime()+1000*60*60*hour;
        date.setTime(l);
        return DateToStr(date, "yyyyMMdd");
    }
@@ -3194,6 +3267,18 @@
    }
    /**
     * 获取X天后的日期
     * @param date
     * @param beforDays 明天 -1  昨天 1
     * @return
     */
    public static String getBeforDay(Date date,Integer beforDays){
        ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.systemDefault());
        // 转换为java.time.LocalDate,计算x天的日期
        LocalDate dateBeforeDay = zonedDateTime.toLocalDate().minusDays(beforDays);
        return dateBeforeDay.toString();
    }
}