获取时间区间 月份开始|结束 时间列表

/***
     * 获取时间区间 月份开始|结束 时间列表
     * 示例
     *     param
     *         startLocalDateTime 2020-03-02 12:22:22
     *         endLocalDateTime  2020-05-22 18:18:18
     *     return
     *         2020-03-02 12:22:22,2020-03-31 23:59:59.999
     *         2020-04-01 00:00:00,2020-04-30 23:59:59.999
     *         2020-05-01 00:00:00,2020-05-22 18:18:18
     * @param startLocalDateTime
     * @param endLocalDateTime
     * @return
     */
    public static List<LocalDateTime[]> getLocalDateTimeIntervalMonthStartEndTime(LocalDateTime startLocalDateTime,LocalDateTime endLocalDateTime){
        if (startLocalDateTime.isAfter(endLocalDateTime)){
            LocalDateTime temp=startLocalDateTime;
            startLocalDateTime=endLocalDateTime;
            endLocalDateTime=temp;
        }
        List<LocalDateTime[]> localDateTimeList=new ArrayList<>();

        LocalDateTime currDateTime=startLocalDateTime;
        LocalDateTime currMonthEndDateTime = LocalDateTime.of(LocalDate.of(currDateTime.getYear(),currDateTime.getMonth(),currDateTime.getDayOfMonth()), LocalTime.MAX).with(TemporalAdjusters.lastDayOfMonth());

        if (currMonthEndDateTime.isBefore(endLocalDateTime)) {
            localDateTimeList.add(new LocalDateTime[]{currDateTime, currMonthEndDateTime});
        } else {
            localDateTimeList.add(new LocalDateTime[]{startLocalDateTime, endLocalDateTime});
        }

        do{
            currDateTime = LocalDateTime.of(LocalDate.of(currDateTime.getYear(),currDateTime.getMonth(),currDateTime.getDayOfMonth()), LocalTime.MIN).with(TemporalAdjusters.firstDayOfNextMonth());
            currMonthEndDateTime = LocalDateTime.of(LocalDate.of(currDateTime.getYear(),currDateTime.getMonth(),currDateTime.getDayOfMonth()), LocalTime.MAX).with(TemporalAdjusters.lastDayOfMonth());
            if (currMonthEndDateTime.isBefore(endLocalDateTime)) {
                localDateTimeList.add(new LocalDateTime[]{currDateTime, currMonthEndDateTime});
            } else {
                if (currDateTime.isBefore(endLocalDateTime)) {
                    localDateTimeList.add(new LocalDateTime[]{currDateTime, endLocalDateTime});
                }
            }
        }while(currMonthEndDateTime.isBefore(endLocalDateTime));

        return localDateTimeList;
    }
原文地址:https://www.cnblogs.com/panbingqi/p/13807027.html