• 获取需要对比的日期范围方法


    能够做到输入一个日期范围,返回一个需要的日期范围。如果具体需求不一致,可以参考做需修改。

    代码公共部分

    import java.time.LocalDate;
    import java.time.temporal.TemporalAdjusters;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Objects;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    import java.time.LocalDate;
    
    public class DateRangeDTO {
        /**
         * 开始时间范围
         */
        private LocalDate startTime;
    
        /**
         * 结束时间范围
         */
        private LocalDate endTime;
    
        public LocalDate getStartTime() {
            return startTime;
        }
    
        public void setStartTime(LocalDate startTime) {
            this.startTime = startTime;
        }
    
        public LocalDate getEndTime() {
            return endTime;
        }
    
        public void setEndTime(LocalDate endTime) {
            this.endTime = endTime;
        }
    
        @Override
        public String toString() {
            return "DateRangeDTO{" +
                    "startTime=" + startTime +
                    ", endTime=" + endTime +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    环比

    月份为准

    对比的是上个月的数据

    说明传入返回
    对比一整个月2023.06.01-2023.06.302023.05.01-2023.05.31
    对比上月同期2023.06.01-2023.06.072023.05.01-2023.05.07
    对比前一天2023.06.01-2023.06.012023.05.31-2023.05.31
    /**
         * 获取环比时间范围--月份为准
         *
         * @param startTime 开始时间范围
         * @param endTime   结束时间范围
         * @return
         */
    public static DateRangeDTO getQoQDateRangeMonth(LocalDate startTime, LocalDate endTime) {
            DateRangeDTO dateRangeDTO = new DateRangeDTO();
            // 是否同一天
            if (startTime.isEqual(endTime)) {
                dateRangeDTO.setStartTime(startTime.plusDays(-1));
                dateRangeDTO.setEndTime(startTime.plusDays(-1));
                return dateRangeDTO;
            }
    
            // 提取年份
            int startYear = startTime.getYear();
            int endYear = endTime.getYear();
            int differYear = endYear - startYear + 1;
    
            // 每个月份满日期数
            int day = LocalDate.of(startYear, 3, 1).plusDays(-1).getDayOfMonth();
            List<Integer> days = Arrays.asList(31, day, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
            // 提取月份
            int startMonth = startTime.getMonthValue();
            int endMonth = endTime.getMonthValue();
            int differMonth = endMonth - startMonth + 1;
    
            // 提取天数
            int startDay = startTime.getDayOfMonth();
            int endDay = endTime.getDayOfMonth();
            int differDay = endDay - startDay + 1;
    
            // 返回的时间范围
            LocalDate startTimeBefore = startTime.plusMonths(-differMonth);
            LocalDate endTimeBefore = endTime.plusMonths(-differMonth);
            // 是否是当月最后一天
            if (Objects.equals(endDay, days.get(endMonth - 1))) {
                endTimeBefore = endTimeBefore.with(TemporalAdjusters.lastDayOfMonth());
            }
    
            dateRangeDTO.setStartTime(startTimeBefore);
            dateRangeDTO.setEndTime(endTimeBefore);
            return dateRangeDTO;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    日期为准

    对比的是前几天的数据,但是如果选择是月份满了就是对比一个月

    说明传入返回
    对比的是上个一样的天数2023.06.10-2023.06.142023.06.05-2023.06.09,
    对比的是上个一样的天数2023.06.10-2023.06.202023.05.30-2023.06.09
    对比月份2023.06.01-2023.06.302023.05.01-2023.05.31
    /**
         * 获取环比时间范围--日期为准
         * @param startTime  开始时间范围
         * @param endTime  结束时间范围
         * @return
         */
        public static DateRangeDTO getQoQDateRangeDay(LocalDate startTime, LocalDate endTime) {
            // 提取年份
            int startYear = startTime.getYear();
            int endYear = endTime.getYear();
            int differYear = endYear - startYear + 1;
    
            // 每个月份满日期数
            int day = LocalDate.of(startYear, 3, 1).plusDays(-1).getDayOfMonth();
            List<Integer> days = Arrays.asList(31, day, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
            // 提取月份
            int startMonth = startTime.getMonthValue();
            int endMonth = endTime.getMonthValue();
            int differMonth = endMonth - startMonth + 1;
    
            // 提取天数
            int startDay = startTime.getDayOfMonth();
            int endDay = endTime.getDayOfMonth();
            int differDay = endDay - startDay + 1;
    
            // 返回的时间范围
            LocalDate startTimeBefore;
            LocalDate endTimeBefore = startTime.plusDays(-1);
    
            // 判断开始范围是第一天 和 结束范围都为当月最后一天
            boolean aBoolean = Objects.equals(startDay, 1) && Objects.equals(endDay, days.get(endMonth - 1));
            // 传递的时间范围在同一个月内
            if (Objects.equals(differMonth, 1) && Objects.equals(differYear, 1)) {
                // 【开始范围是第一天 和 结束范围都为最后一天】 就为 上个月  【反之】  反推天数
                startTimeBefore = aBoolean ? startTime.plusMonths(-1) : startTime.plusDays(-differDay);
            } else  {
                // 【月份日期都是满的】 就推月份 【反之】 反推天数
                startTimeBefore = aBoolean ? startTime.plusMonths(-differMonth) : startTime.plusDays(-differDay);
            }
    
            DateRangeDTO dateRangeDTO = new DateRangeDTO();
            dateRangeDTO.setStartTime(startTimeBefore);
            dateRangeDTO.setEndTime(endTimeBefore);
    
            return dateRangeDTO;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
  • 相关阅读:
    酷开科技OS——Coolita,让智能大屏走向国际
    PDF删除页面免费的方法有什么?PDF怎么删除页面的技巧你不能错过
    刷题分析工具
    【阿里云】域名解析 & Tomcat绑定域名
    基于Java web的校园滴滴代驾管理系统 毕业设计-附源码260839
    【前端实例代码】Html5+css3创建拟物风格昏昏欲睡的云朵动画网页效果~前端开发网页设计基础入门教程~适合初学者~超简单~
    新一代分布式实时流处理引擎Flink入门实战操作篇
    测试/开发程序员的思考,突破变得更强......
    JDBC-03:PreparedStatement如何实现对数据库的增删改查操作
    贪心算法——知识点总结
  • 原文地址:https://blog.csdn.net/weixin_46488534/article/details/132690860