Java 将一段时间以周、月、季分割

本文所有使用的时间戳均为毫秒级 得到的集合区间也是毫秒级的时间戳

  • 按周分割时间

    1. 代码如下:
      @Test
      public void timeSplitByWeek() {
          // 1.开始时间 2019-06-09 13:16:04
          Long startTime = 1560057364000L;
          // 2.结束时间 2019-07-09 13:16:04
          Long endTime = 1562649364000L;
          // 3.开始时间段区间集合
          List beginDateList = new ArrayList();
          // 4.结束时间段区间集合
          List endDateList = new ArrayList();
          // 5.调用工具类
          MyUtils.getIntervalTimeByWeek(startTime, endTime, beginDateList, endDateList);
          // 6.打印输出
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          for (int i = 0; i < beginDateList.size(); i++) {
              Long beginStr = beginDateList.get(i);
              Long endStr = endDateList.get(i);
              String begin1 = sdf.format(new Date(beginStr));
              String end1 = sdf.format(new Date(endStr));
              System.out.println("第" + i + "段时间区间:" + begin1 + "-------" + end1);
          }
      }
      
    2. 运行结果如下图所示:


      以周分割时间
  • 按月分割时间

    1. 代码如下:
      @Test
      public void timeSplitByMonth() {
          // 1.开始时间 2017-02-03 13:16:04
          Long startTime = 1486098964000L;
          // 2.结束时间 2019-07-03 13:16:05
          Long endTime = 1562130965000L;
          // 3.开始时间段区间集合
          List beginDateList = new ArrayList();
          // 4.结束时间段区间集合
          List endDateList = new ArrayList();
          // 5.调用工具类
          MyUtils.getIntervalTimeByMonth(startTime, endTime, beginDateList, endDateList);
          // 6.打印输出
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          for (int i = 0; i < beginDateList.size(); i++) {
              Long beginStr = beginDateList.get(i);
              Long endStr = endDateList.get(i);
              String begin1 = sdf.format(new Date(beginStr));
              String end1 = sdf.format(new Date(endStr));
              System.out.println("第" + i + "段时间区间:" + begin1 + "-------" + end1);
          }
      }
      
    2. 运行结果如下图所示:


      以月分割时间
  • 按季分割时间

    1. 代码如下:
      @Test
      public void timeSplitByQuarter() {
          // 1.开始时间 2018-12-09 13:16:04
          Long startTime = 1544332564000L;
          // 2.结束时间 2019-12-09 13:16:04
          Long endTime = 1575868564000L;
          // 3.开始时间段区间集合
          List beginDateList = new ArrayList();
          // 4.结束时间段区间集合
          List endDateList = new ArrayList();
          // 5.调用工具类
          MyUtils.getIntervalTimeByQuarter(startTime, endTime, beginDateList, endDateList);
          // 6.打印输出
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          for (int i = 0; i < beginDateList.size(); i++) {
              Long beginStr = beginDateList.get(i);
              Long endStr = endDateList.get(i);
              String begin1 = sdf.format(new Date(beginStr));
              String end1 = sdf.format(new Date(endStr));
              System.out.println("第" + i + "段时间区间:" + begin1 + "-------" + end1);
          }
      }
      
    2. 运行结果如下图所示:


      以季分割时间
  • 工具类MyUtils代码如下所示:

    public class MyUtils {
        /**
        * 以季度分割时间段
        * 此处季度是以 12-2月   3-5月   6-8月  9-11月 划分  
        * @param startTime     开始时间戳(毫秒)
        * @param endTime       结束时间戳(毫秒)
        * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应
        * @param endDateList   结束段时间戳 和 开始段时间戳 一一对应
        */
        public static void getIntervalTimeByQuarter(Long startTime, Long endTime, List beginDateList, List endDateList) {
            Date startDate = new Date(startTime);
            Date endDate = new Date(endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            int month = calendar.get(Calendar.MONTH) + 1;
            switch (month) {
                case 12:
                case 3:
                case 6:
                case 9:
                    addTime(beginDateList, endDateList, startDate, endDate, calendar, 3);
                    break;
                case 1:
                case 4:
                case 7:
                case 10:
                    addTime(beginDateList, endDateList, startDate, endDate, calendar, 2);
                    break;
                case 2:
                case 5:
                case 8:
                case 11:
                    addTime(beginDateList, endDateList, startDate, endDate, calendar, 1);
                    break;
            }
        }
    
        private static void addTime(List beginDateList, List endDateList, Date startDate, Date endDate, Calendar calendar, int i) {
            beginDateList.add(startDate.getTime());
            calendar.add(Calendar.MONTH, i);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.DATE, -1);
            calendar.set(Calendar.HOUR_OF_DAY, 13);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            if (calendar.getTimeInMillis() > endDate.getTime()) {
                endDateList.add(endDate.getTime());
    
            } else {
                endDateList.add(calendar.getTimeInMillis());
                while (calendar.getTimeInMillis() < endDate.getTime()) {
                    calendar.add(Calendar.DATE, 1);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    beginDateList.add(calendar.getTimeInMillis());
                    calendar.add(Calendar.MONTH, 3);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.add(Calendar.DATE, -1);
                    calendar.set(Calendar.HOUR_OF_DAY, 13);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    if (calendar.getTimeInMillis() < endDate.getTime()) {
                        endDateList.add(calendar.getTimeInMillis());
                    } else {
                        endDateList.add(endDate.getTime());
                    }
                }
            }
        }
    
        /**
        * 以周分割时间段
        *
        * @param startTime     开始时间戳(毫秒)
        * @param endTime       结束时间戳(毫秒)
        * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应
        * @param endDateList   结束段时间戳 和 开始段时间戳 一一对应
        */
        public static void getIntervalTimeByWeek(Long startTime, Long endTime, List beginDateList, List endDateList) {
            Date startDate = new Date(startTime);
            Date endDate = new Date(endTime);
            SimpleDateFormat sdw = new SimpleDateFormat("E");
            Calendar calendar = Calendar.getInstance();
            String begin = sdw.format(startDate);
            calendar.setTime(startDate);
            beginDateList.add(calendar.getTimeInMillis());
            if ("星期一".equals(begin)) {
                addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar);
            } else {
                if ("星期日".equals(sdw.format(startDate))) {
                    Calendar special = Calendar.getInstance();
                    special.setTime(startDate);
                    special.set(Calendar.HOUR_OF_DAY, 23);
                    special.set(Calendar.MINUTE, 59);
                    special.set(Calendar.SECOND, 59);
                    endDateList.add(special.getTime().getTime());
                }
                addTimeStamp(beginDateList, endDateList, startDate, endDate, sdw, calendar);
            }
        }
    
        private static void addTimeStamp(List beginDateList, List endDateList, Date startDate, Date endDate, SimpleDateFormat sdw, Calendar calendar) {
            while (startDate.getTime() < endDate.getTime()) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                startDate = calendar.getTime();
                if ("星期一".equals(sdw.format(startDate))) {
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    beginDateList.add(calendar.getTimeInMillis());
                } else if ("星期日".equals(sdw.format(startDate)) || startDate.getTime() >= endDate.getTime()) {
                    if (startDate.getTime() <= endDate.getTime()) {
                        calendar.set(Calendar.HOUR_OF_DAY, 23);
                        calendar.set(Calendar.MINUTE, 59);
                        calendar.set(Calendar.SECOND, 59);
                        endDateList.add(calendar.getTimeInMillis());
                    } else {
                        endDateList.add(endDate.getTime());
                    }
                }
            }
        }
    
        /**
        * 按照月份分割一段时间
        *
        * @param startTime     开始时间戳(毫秒)
        * @param endTime       结束时间戳(毫秒)
        * @param beginDateList 开始段时间戳 和 结束段时间戳 一一对应
        * @param endDateList   结束段时间戳 和 开始段时间戳 一一对应
        */
        public static void getIntervalTimeByMonth(Long startTime, Long endTime, List beginDateList, List endDateList) {
            Date startDate = new Date(startTime);
            Date endDate = new Date(endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            beginDateList.add(calendar.getTimeInMillis());
            while (calendar.getTimeInMillis() < endDate.getTime()) {
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.add(Calendar.DATE, -1);
                calendar.set(Calendar.HOUR_OF_DAY, 13);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                if(calendar.getTimeInMillis() < endDate.getTime()){
                    endDateList.add(calendar.getTimeInMillis());
                } else {
                    endDateList.add(endDate.getTime());
                    break;
                }
                calendar.add(Calendar.DATE, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                beginDateList.add(calendar.getTimeInMillis());
            }
        }
    }
    
    

你可能感兴趣的:(Java 将一段时间以周、月、季分割)