根据开始和结束日期,获取每一天和每个月的开始和结束日期的list

发布于:2025-03-11 ⋅ 阅读:(13) ⋅ 点赞:(0)

获取开始日期与结束日期之间每天的list

/**
     * 根据传入的开始时间和结束时间,筛选出所有的天的list;
     *
     * @param startTime
     * @param endTime
     */
    public Map<String, List<String>> fetchDayListBetweenStartAndEnd(String startTime, String endTime) {
        // 创建map
        Map<String, List<String>> map = Maps.newHashMap(); // guava的工具类
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 开始时间列表
        List<String> startTimeList = Lists.newArrayList();// guava的工具类
        // 结束时间列表
        List<String> endTimeList = Lists.newArrayList(); // guava的工具类
        Date startDate = null;
        Date checkDate = null;

        try {
            startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            while (startDate.before(endDate) && month <= 11) {
                final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, last);
                Date lastDayOfMonth = cal.getTime();
                if (lastDayOfMonth.after(endDate)) {
                    checkDate = endDate;
                } else {
                    checkDate = lastDayOfMonth;
                }

                // 遍历每个月的日期,并添加到start和end的list中
                cal.setTime(startDate);
                while (startDate.before(checkDate)) {
                    startTimeList.add(sdf.format(startDate));
                    endTimeList.add(sdf.format(startDate));
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                    startDate = cal.getTime();
                }
                // 添加checkDate到start和end的list中
                startTimeList.add(sdf.format(checkDate));
                endTimeList.add(sdf.format(checkDate));

                // 当month为12时,遍历下一年
                month = month + 1;
                if (month == 12) {
                    year = year + 1;
                    month = 0;
                }
                cal.set(year, month, 1);
                startDate = cal.getTime();

                // 当endTime是月份的第一天时,设置startTime;
                startTime = sdf.format(startDate);
            }

            // 比较startTime和endDate:相等时,添加到start和end的list中
            if (startTime.equals(sdf.format(endDate))) {
                startTimeList.add(startTime);
                endTimeList.add(sdf.format(endDate));
            }
            if (!startTimeList.isEmpty() && !endTimeList.isEmpty()) {
                map.put("dayTimeList", startTimeList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

获取开始日期与结束日期之间每个月的开始日期和结束日期的list

/**
     * 根据开始时间和结束时间,列出每个月的开始日期和结束日期,并分别放到startTimeList和endTimeList
     *
     * @param startTime
     * @param endTime
     */
    public Map<String, List<String>> fetchMonthFirstLastList(String startTime, String endTime) {
        // 创建map
        Map<String, List<String>> map = Maps.newHashMap(); // guava的工具类
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 开始时间列表
        List<String> startTimeList = Lists.newArrayList();// guava的工具类
        // 结束时间列表
        List<String> endTimeList = Lists.newArrayList(); // guava的工具类

        Date startDate = null;

        try {
            startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);

            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            // 当开始日期在结束时期之前,添加日期到startTimeList和endTimeList中
            while (startDate.before(endDate) && month <= 11) {
                final int first = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, first);
                Date firstDayOfMonth = cal.getTime();
                if (firstDayOfMonth.before(startDate)) {
                    startTimeList.add(sdf.format(startDate));
                } else {
                    startTime = sdf.format(firstDayOfMonth);
                    startTimeList.add(startTime);
                }
                final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, last);
                Date lastDayOfMonth = cal.getTime();
                if (lastDayOfMonth.after(endDate)) {
                    endTimeList.add(sdf.format(endDate));
                } else {
                    endTimeList.add(sdf.format(lastDayOfMonth));
                }

                month += 1;
                if (month == 12) {
                    year = year + 1;
                    month = 0;
                }
                cal.set(year, month, 1);
                startDate = cal.getTime();
                // 当endTime是月份第一天时,设置startTime,再利用startTime.equals(sdf.format(endDate));
                // 添加startTime和endTime到list中
                startTime = sdf.format(startDate);
            }
            // 当结束日期是在月初第一天时,把开始日期和结束日期都添加上去
            if (startTime.equals(sdf.format(endDate))) {
                startTimeList.add(startTime);
                endTimeList.add(sdf.format(endDate));
            }

            // 添加
            if (!startTimeList.isEmpty() && !endTimeList.isEmpty()) {
                map.put("startTimeList", startTimeList);
                map.put("endTimeList", endTimeList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }