获取自然周 自然月 区间

 public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      /**
       * @Description: 获取时间段内所有自然周,参数格式为:yyyy-MM-dd
       * @Param: [startDate, endDate]
       * @Return: java.util.List<Test.DateVo>
       */
      public static List<CycleTimeVo> getBetweenWeeks(String startDate, String endDate) throws ParseException {
            List<String> listWeekOrMonth = new ArrayList<String>();
            List<CycleTimeVo> dateVoList = new ArrayList<CycleTimeVo>();
            Date sDate = sdf.parse(startDate);// 定义起始日期
            Calendar sCalendar = Calendar.getInstance();
            sCalendar.setFirstDayOfWeek(Calendar.MONDAY);
            sCalendar.setTime(sDate);
            Date eDate = sdf.parse(endDate);// 定义结束日期
            Calendar eCalendar = Calendar.getInstance();
            eCalendar.setFirstDayOfWeek(Calendar.MONDAY);
            eCalendar.setTime(eDate);
            boolean bool = true;
            while (sCalendar.getTime().getTime() < eCalendar.getTime().getTime()) {
                  if (bool || sCalendar.get(Calendar.DAY_OF_WEEK) == 2 || sCalendar.get(Calendar.DAY_OF_WEEK) == 1) {
                        listWeekOrMonth.add(sdf.format(sCalendar.getTime()));
                        bool = false;
                  }
                  sCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            listWeekOrMonth.add(sdf.format(eCalendar.getTime()));
            if (listWeekOrMonth.size() % 2 != 0) {
                  listWeekOrMonth.add(sdf.format(eCalendar.getTime()));
            }
            for (int i = 0; i < listWeekOrMonth.size() - 1; i++) {
                  if (i % 2 == 0) {
                        CycleTimeVo dateVo = new CycleTimeVo();
                        dateVo.setStartTimeStr(listWeekOrMonth.get(i));
                        dateVo.setEndTimeStr(listWeekOrMonth.get(i + 1));
                        dateVoList.add(dateVo);
                  }
            }
            return dateVoList;
      }

      /**
       * 获取自然月
       * @param minDate
       * @param maxDate
       * @return
       * @throws ParseException
       */
      public static List<CycleTimeVo> getMonthBetween(String minDate, String maxDate) throws ParseException {
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;

            LinkedList<CycleTimeVo> CycleTimeList=new LinkedList();
            while (curr.before(max)) {

                  CycleTimeVo cycleTimeVo=new CycleTimeVo();
                  cycleTimeVo.setStartTimeStr(sdf.format(curr.getTime()));
                  String monthEnd = getMonthEnd(sdf.format(curr.getTime()));
                  cycleTimeVo.setEndTimeStr(monthEnd);
                  CycleTimeList.add(cycleTimeVo);
                  //result.add(sdf.format(curr.getTime()));
                  curr.add(Calendar.MONTH, 1);
            }
            CycleTimeList.getFirst().setStartTimeStr(minDate);
            CycleTimeList.getLast().setEndTimeStr(maxDate);
            return CycleTimeList;
      }


      public static String getMonthEnd(String  time) throws ParseException {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date= simpleDateFormat.parse(time);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            //设置为当月最后一天
            c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
            //将小时至23
            c.set(Calendar.HOUR_OF_DAY, 23);
            //将分钟至59
            c.set(Calendar.MINUTE, 59);
            //将秒至59
            c.set(Calendar.SECOND,59);
            //将毫秒至999
            c.set(Calendar.MILLISECOND, 999);
            // 获取本月最后一天的时间
            return simpleDateFormat.format(c.getTime());
      }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值