Java按时间范围拆分天、周、月、年

x33g5p2x  于2021-03-23 发布在 Java  
字(5.0k)|赞(0)|评价(0)|浏览(562)

需求:输入开始时间和结束时间,输出天、周、月、年列表。

时间范围按天拆分

public static List<String> dayLists(String startDt, String endDt){
        List<String> dayList = Lists.newArrayList();
        if(StringUtils.isBlank(startDt) || StringUtils.isBlank(endDt)){
            return dayList;
        }
        LocalDate startDate = LocalDate.parse(startDt, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate endDate = LocalDate.parse(endDt, DateTimeFormatter.ofPattern("yyyyMMdd"));
        //如果结束日期小于开始日期,直接返回
        if(endDate.isBefore(startDate)){
            return dayList;
        }
        while (startDate.isBefore(endDate) || startDate.equals(endDate)){
            dayList.add(startDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            startDate = startDate.plusDays(1);
        }
        return dayList;
    }

输入: startDt:20191130 endDt:20200130
输出:

["20191130","20191201","20191202","20191203","20191204","20191205","20191206","20191207","20191208","20191209","20191210","20191211","20191212","20191213","20191214","20191215","20191216","20191217","20191218","20191219","20191220","20191221","20191222","20191223","20191224","20191225","20191226","20191227","20191228","20191229","20191230","20191231","20200101","20200102","20200103","20200104","20200105","20200106","20200107","20200108","20200109","20200110","20200111","20200112","20200113","20200114","20200115","20200116","20200117","20200118","20200119","20200120","20200121","20200122","20200123","20200124","20200125","20200126","20200127","20200128","20200129","20200130"]

时间范围按周拆分

public static List<String> weekLists(String startDt, String endDt){
        List<String> weekList = Lists.newArrayList();
        if(StringUtils.isBlank(startDt) || StringUtils.isBlank(endDt)){
            return weekList;
        }
        try {
            Calendar startDate = Calendar.getInstance();
            startDate.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
            startDate.setMinimalDaysInFirstWeek(4);//
            System.out.println("startDt:" + startDt +",endDt:" + endDt );
            startDate.setTime(new SimpleDateFormat("yyyyMMdd").parse(startDt));
            Calendar endDate = Calendar.getInstance();
            endDate.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
            endDate.setMinimalDaysInFirstWeek(4);//
            endDate.setTime(new SimpleDateFormat("yyyyMMdd").parse(endDt));
            if(startDate.compareTo(endDate) > 0){
                return weekList;
            }
            startDate.set(Calendar.DAY_OF_WEEK,2); //设置为周一
            while (startDate.compareTo(endDate) < 0) {
                int beforeStartWeek = startDate.get(Calendar.WEEK_OF_YEAR);
                int beforeYear = startDate.get(Calendar.YEAR);
                startDate.add(Calendar.WEEK_OF_YEAR,1);//周数新增一天
                startDate.set(Calendar.DAY_OF_WEEK,2); //设置为周一
                if(beforeYear < startDate.get(Calendar.YEAR)){
                    if(beforeStartWeek <10 ){
                        beforeYear = startDate.get(Calendar.YEAR);
                    }
                }
                weekList.add(String.format("%dW%02d", beforeYear,beforeStartWeek));
            }
        } catch (ParseException e) {
            System.out.println("日期输入格式不对");
        }
        return weekList;
    }

输入: startDt:20191130 endDt:20200130
输出:

["2019W48","2019W49","2019W50","2019W51","2019W52","2020W01","2020W02","2020W03","2020W04","2020W05"]

时间范围按月拆分

public static List<String> monthLists(String startDt, String endDt){
        List<String> monthList = Lists.newArrayList();
        if(StringUtils.isBlank(startDt) || StringUtils.isBlank(endDt)){
            return monthList;
        }
        LocalDate startDate = LocalDate.parse(startDt, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate endDate = LocalDate.parse(endDt, DateTimeFormatter.ofPattern("yyyyMMdd"));
        //如果结束日期小于开始日期,直接返回
        if(endDate.isBefore(startDate)){
            return monthList;
        }
        while (startDate.getYear()*100 + startDate.getMonthValue() <= endDate.getYear()*100 + endDate.getMonthValue()){
            monthList.add(startDate.format(DateTimeFormatter.ofPattern("yyyyMM")));
            startDate = startDate.plusMonths(1);
        }
        return monthList;
    }

输入: startDt:20191130 endDt:20200130
输出:

["201911","201912","202001"]

时间范围按年拆分

public static List<String> yearLists(String startDt, String endDt){
        List<String> yearList = Lists.newArrayList();
        if(StringUtils.isBlank(startDt) || StringUtils.isBlank(endDt)){
            return yearList;
        }
        LocalDate startDate = LocalDate.parse(startDt, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate endDate = LocalDate.parse(endDt, DateTimeFormatter.ofPattern("yyyyMMdd"));
        //如果结束日期小于开始日期,直接返回
        if(endDate.isBefore(startDate)){
            return yearList;
        }
        while (startDate.getYear() <= endDate.getYear()){
            yearList.add(startDate.format(DateTimeFormatter.ofPattern("yyyy")));
            startDate = startDate.plusYears(1);
        }
        return yearList;
}

输入: startDt:20191130 endDt:20200130
输出:

["2019","2020"]

相关文章

热门文章

更多