当前位置: 首页 > article >正文

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

获取开始日期与结束日期之间每天的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;
    }

http://www.kler.cn/a/584800.html

相关文章:

  • 深度对话:AI界的奥本海默与通用人工智能(AGI)的未来
  • 如何在Futter开发中做性能优化?
  • 前端面试:React生态有哪些?
  • Unity开发的抖音小游戏接入抖音开放平台中的流量主(抖音小游戏接入广告)
  • ubuntu 设置允许root远程登录
  • 使用联核科技四向穿梭车能给企业带来哪些效益?
  • CSS-基础选择器,字体属性,文本属性介绍
  • 【MySQL】基本操作 —— DDL
  • JVM 解释器和即时编译器有什么区别?
  • USER与多组织关联的SQL查询以及几个关键函数用法
  • Redis 单线程架构:化繁为简的性能哲学
  • Android 页面封装实战:打造高复用、灵活的 Scaffold 式布局与事件处理
  • vscode 配置golang开发环境
  • 《今日AI-人工智能-编程日报》
  • 从零开始写3D游戏引擎(开发环境VS2022+OpenGL)之九点五 编写运动摄像机镜头的源代码 细嚼慢咽逐条读代码系列
  • flutter 开发中的tips 【持续更新】
  • iframe 内事件冒泡
  • 数据标注质量对AI模型质量的影响分析
  • 微信小程序-实现锚点跳转,页面加载后自动跳转、点击跳转到指定位置
  • vue判断视频链接是否有效