LocalDate类型的相关使用

  • minusDays(int i):对当前日期减 i 天。
  • plusDays(int i):对当前日期加 i 天
  • A.isBefore(B):A天在B天之前
  • A.isAfter(B):A天在B天之后

注意事项:

  • 使用isBefore()isAfter()判断某个时间是否在区间中时,要扩大一天的区间范围。

示例:

  • 10-01是属于十月的范围的,但是如果在判断中直接用十月的起始去判断时,会认为不属于该范围,也就是说默认是不包含起始的,所以需要对开始进行minusDays(1),对结束时间进行plusDays(1)
 @Test
    public void test9(){
        String today = "2023-10-01";
        String start = "2023-10-01";
        String end = "2023-10-31";
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate time = LocalDate.parse(today, pattern);
        LocalDate startCurrent = LocalDate.parse(start,pattern);
        LocalDate endCurrent = LocalDate.parse(end,pattern);

        if (time.isAfter(startCurrent) && time.isBefore(endCurrent)) {
            System.out.println("today在十月范围中");
        }else{
            System.out.println("不在十月范围中");
        }
    }

结果:

LocalDate类型的相关使用_第1张图片

示例:

  • 获取十天范围
  • 获取十月范围
  • 获取十年范围
@Test
    public void test8() {
        LocalDate today = LocalDate.now();
        List<LocalDate> tenDayRangle = getTenDayRangle(today);
        System.out.println(tenDayRangle);
        System.out.println("=============================");

        List<YearMonth> tenMonthsRangle = getTenMonthsRangle(today);
        System.out.println(tenMonthsRangle);
        System.out.println("=============================");


        Map<String, Object> tenYearsRangle = getTenYearsRangle(today);
        System.out.println(tenYearsRangle);
        System.out.println("=============================");
    }
    // 获取天范围[11,12,13,14,15...]
    private static List<LocalDate> getTenDayRangle(LocalDate today) {
        List<LocalDate> dates = new ArrayList<>();
        LocalDate startDate = today.minusDays(9);

        while (startDate.isBefore(today)) {
            dates.add(startDate);
            startDate = startDate.plusDays(1);
        }
        dates.add(today);
        return dates;
    }

    // 获取月份范围[11,12,1,2,3,4,5....]
    // YearMonth.getMonth()获取月份
    private static List<YearMonth> getTenMonthsRangle(LocalDate today) {
        List<YearMonth> months = new ArrayList<>();
        LocalDate currentDate = today.minusMonths(9);
        LocalDate endDate = today;

        while (currentDate.isBefore(endDate) || currentDate.equals(endDate)) {
            YearMonth month = YearMonth.from(currentDate);
            months.add(month);
            currentDate = currentDate.plusMonths(1);
        }

        return months;
    }

    // 获取十年范围[2014,2015,2017....]
    private static Map<String, Object> getTenYearsRangle(LocalDate today) {
        List<Year> list = new ArrayList();
        Map<String, Object> map = new HashMap();

        LocalDate localDate = null;
        Year year = null;
        for (int i = 9; i >= 0; i--) {
            localDate = today.minusYears(i);
            year = Year.from(localDate);
            list.add(year);

            // 十年前的开始时间
            if (i == 9) {
                map.put("startYear", year.atDay(1));
            }

            // 结束时间
            if (i == 0) {
                localDate = today.plusYears(1);
                year = Year.from(localDate);
                map.put("endYear", year.atDay(1));
            }
        }
        map.put("yearRange", list);
        return map;
    }

结果:

在这里插入图片描述

你可能感兴趣的:(Java,java)