美文网首页日期
根据日、星期、月、年拆分周期

根据日、星期、月、年拆分周期

作者: 星钻首席小管家 | 来源:发表于2022-03-25 14:05 被阅读0次

1.日期根据日、星期、月、年拆分周期

/**
     * 根据传入的参数,来对日期区间进行拆分,返回拆分后的日期List
     * @param statisticsType
     * @param map
     * @editor
     * @editcont
     */
    public static List<String> doDateByStatisticsType(String statisticsType, Map<String, Object> map) throws ParseException{
        List<String> listWeekOrMonth = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = (String)map.get("startDate");
        String endDate = (String)map.get("endDate");
        Date sDate = dateFormat.parse(startDate);
        Calendar sCalendar = Calendar.getInstance();
        sCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        sCalendar.setTime(sDate);
        Date eDate = dateFormat.parse(endDate);
        Calendar eCalendar = Calendar.getInstance();
        eCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        eCalendar.setTime(eDate);
        boolean bool =true;
        if(statisticsType.equals("day")){
            while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
                listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
                sCalendar.add(Calendar.DATE,1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }else if(statisticsType.equals("week")){
            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(dateFormat.format(sCalendar.getTime()));
                    bool = false;
                }
                sCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }else if(statisticsType.equals("month")){
            while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
                if(bool||sCalendar.get(Calendar.DAY_OF_MONTH)==1||sCalendar.get(Calendar.DAY_OF_MONTH)==sCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)){
                    listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
                    bool = false;
                }
                sCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }else{
            while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
                if(bool||sCalendar.get(Calendar.DAY_OF_YEAR)==1||sCalendar.get(Calendar.DAY_OF_YEAR)==sCalendar.getActualMaximum(Calendar.DAY_OF_YEAR)){
                    listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
                    bool = false;
                }
                sCalendar.add(Calendar.DAY_OF_YEAR, 1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }

        return listWeekOrMonth;
    }

2.简单使用

String statisticsType = null;
        switch (type){
            case 1:
                statisticsType = "day";
                break;
            case 2:
                statisticsType = "week";
                break;
            case 3:
                statisticsType = "month";
                break;
            default:
                statisticsType = "year";
        }

        Map<String, Object> map = new HashMap<>();
        map.put("startDate", DateUtils.format(beginDate));
        map.put("endDate", DateUtils.format(endDate));

        try {
            //按类型拆分日期
            List<String> list = DateUtils.doDateByStatisticsType(statisticsType, map);

            if(type==1){
                for (String s : list) {
                    Date time1 = DateUtils.getStartDate(s);
                    Date time2 = DateUtils.getEndDate(s);
                    //todo 日周期的业务操作
                }
            }else {
                for(int i=0;i<list.size();i+=2){
                    String time1 = list.get(i);
                    String time2 = list.get(i + 1);
                    //todo 星期、月、年
                }
            }
        }

相关文章

网友评论

    本文标题:根据日、星期、月、年拆分周期

    本文链接:https://www.haomeiwen.com/subject/odffjrtx.html