zhangherong
2025-05-06 665ffec07abac9fa14e7613fe1c73922a537ff77
lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java
@@ -6,11 +6,10 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.time.format.DateTimeFormatter;
import java.util.*;
import org.jeecg.common.constant.SymbolConstant;
import org.springframework.util.StringUtils;
@@ -783,4 +782,91 @@
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
    /**
     * 获取本月第一天
     *
     * @return
     */
    public static LocalDate getFirstOfMonth() {
        LocalDate localDate = LocalDate.now();
        return localDate.withDayOfMonth(1);
    }
    /**
     * 获取本月第一天
     *
     * @return
     */
    public static LocalDate getFirstOfMonth(LocalDate localDate) {
        return localDate.withDayOfMonth(1);
    }
    /**
     * 获取两个月份中间的所有月份,包含开始和结束月份
     *
     * @param startMonth
     * @param endMonth
     * @return
     */
    public static List<String> getMonthsBetween(String startMonth, String endMonth) {
        List<String> months = new ArrayList<>();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 解析起始和结束月份
        YearMonth start = YearMonth.parse(startMonth, formatter);
        YearMonth end = YearMonth.parse(endMonth, formatter);
        // 确保start <= end 如果不是 则进行交换
        if (start.isAfter(end)) {
            YearMonth temp = start;
            start = end;
            end = temp;
        }
        // 循环添加月份
        YearMonth current = start;
        while (!current.isAfter(end)) {
            months.add(current.format(formatter));
            current = current.plusMonths(1);
        }
        return months;
    }
    /**
     * 获取两个月份中间的所有月份,包含开始和结束月份
     *
     * @param startMonth
     * @param endMonth
     * @return
     */
    public static List<String> getMonthsBetween(LocalDate startMonth, LocalDate endMonth) {
        List<String> months = new ArrayList<>();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 解析起始和结束月份
        YearMonth start = YearMonth.of(startMonth.getYear(), startMonth.getMonth());
        YearMonth end = YearMonth.of(endMonth.getYear(), endMonth.getMonth());
        // 确保start <= end 如果不是 则进行交换
        if (start.isAfter(end)) {
            YearMonth temp = start;
            start = end;
            end = temp;
        }
        // 循环添加月份
        YearMonth current = start;
        while (!current.isAfter(end)) {
            months.add(current.format(formatter));
            current = current.plusMonths(1);
        }
        return months;
    }
}