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

【知识分享】Java获取全年每个月的有几周且每周是几号到几号

加哥本周给大家分享一期怎么用java把全年每个月有几周,本周是几号到几号的工具类。便于大家根据需求获取想要的形式进行改造。话不多说,直接给大家上代码。

package com.techfantasy.common.utils;

import com.techfantasy.common.entity.DateRange;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.*;

/**
 * @Author feng
 * @Date 2023/8/510:21
 * @Version 1.0
 * @Param
 * 时间分割工具类
 */
public class DateSplitUtil {
    private DateSplitUtil() {
        throw new IllegalStateException("请通过静态方法调用!");
    }

    /**
     * 切分时间,并且获取切分后的时间段集合
     *
     * @param type      分割类型:按年、按月、按周、按日
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 切分后的时间段集合
     */
    public static List<DateRange> splitAndGetByType(int type, LocalDateTime startTime, LocalDateTime endTime) {
        if (type == 1) {
            // 按年切分
            return splitDateRangeByYear1(startTime, endTime);
        } else if (type == 2) {
            // 按月切分
            return splitDateRangeByMonth(startTime, endTime);
        } else if (type == 3) {
            // 按周切分
            return splitDateRangeByWeek(startTime, endTime);
        } else if (type == 4) {
            //按日切分
            return splitDateRangeByDay(startTime, endTime);
        } else if (type == 5) {
            //一次性
            List<DateRange> dateRangeList = new ArrayList<>();
            DateRange dateRange = new DateRange();
            dateRange.setBegin(startTime);
            dateRange.setEnd(endTime);
            dateRangeList.add(dateRange);
            return dateRangeList;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 切分时间,并且获取某一轮的时间对象
     *
     * @param type      分割类型:按年、按月、按周、按日
     * @param turn      时间段(分割后的时间轮数,如将2021-10-01~2022-10-01,按月分割时,就能得到12轮时间段)
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 第turn段的时间对象
     */
    public static DateRange splitAndGetByTurn(int type, long turn, LocalDateTime startTime, LocalDateTime endTime) {
        //按年切分
        if (type == 1) {
            List<DateRange> dateRangeList = splitDateRangeByYear(startTime, endTime);
            return getRangeByTurn(dateRangeList, turn);
        } else if (type == 2) {
            // 按月切分
            List<DateRange> dateRangeList = splitDateRangeByMonth(startTime, endTime);
            return getRangeByTurn(dateRangeList, turn);
        } else if (type == 3) {
            // 按周切分
            List<DateRange> dateRangeList = splitDateRangeByWeek(startTime, endTime);
            return getRangeByTurn(dateRangeList, turn);
        } else if (type == 4) {
            //按日切分
            List<DateRange> dateRangeList = splitDateRangeByDay(startTime, endTime);
            return getRangeByTurn(dateRangeList, turn);
        } else if (type == 5) {
            //一次性
            DateRange dateRange = new DateRange();
            dateRange.setBegin(startTime);
            dateRange.setEnd(endTime);
            return dateRange;
        } else {
            return new DateRange();
        }
    }


    /**
     * 按年分割
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 分割后的时间段集合
     */
    private static List<DateRange> splitDateRangeByYear(LocalDateTime startTime, LocalDateTime endTime) {
        long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
        if (seconds <= 0) {
            return new ArrayList<>();
        }
        //轮数
        long turnNum = 0;
        //分割的时间段集合,使用累加算法
        List<DateRange> dateList = new ArrayList<>();
        DateRange range = new DateRange();
        range.setBegin(startTime);
        while (true) {
            turnNum++;
            //年份相等,不再累加
            if (startTime.getYear() == endTime.getYear()) {
                range.setEnd(endTime);
                range.setTurnNum(turnNum);
                dateList.add(range);
                break;
            }
            //将时间调整为该年的第一天 0时 0分 0秒
            startTime = LocalDateTime.of(LocalDate.from(startTime.with(TemporalAdjusters.firstDayOfYear()).plusYears(1)), LocalTime.MIN);
            LocalDateTime tmpBegin = startTime;
            //计算出上一天的最后一秒
            LocalDateTime endDate = tmpBegin.minusSeconds(1);
            range.setEnd(endDate);
            range.setTurnNum(turnNum);
            dateList.add(range);
            //创建新的时间段
            range = new DateRange();
            range.setBegin(tmpBegin);
        }
        return dateList;
    }

    /**
     * 按月分割
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 分割后的时间段集合
     */
    private static List<DateRange> splitDateRangeByMonth(LocalDateTime startTime, LocalDateTime endTime) {

        long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
        if (seconds <= 0) {
            return new ArrayList<>();
        }
        //轮数
        long turnNum = 0;
        //分割的时间段集合,使用累加算法
        List<DateRange> dateList = new ArrayList<>();
        DateRange range = new DateRange();
        range.setBegin(startTime);
        while (true) {
            turnNum++;
            startTime = startTime.plusMonths(1);
            //大于截止日期时,不再累加
            if (startTime.isAfter(endTime)) {
                range.setEnd(endTime);
                range.setTurnNum(turnNum);
                dateList.add(range);
                break;
            }
            //将时间调整为当前月的第一天的 0时 0分 0秒
            startTime = LocalDateTime.of(LocalDate.from(startTime.with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
            LocalDateTime tmpBegin = startTime;
            //计算出上一天的最后一秒
            LocalDateTime endDate = tmpBegin.minusSeconds(1);
            range.setEnd(endDate);
            range.setTurnNum(turnNum);
            dateList.add(range);
            //创建新的时间段
            range = new DateRange();
            range.setBegin(tmpBegin);
        }
        return dateList;
    }
    /**
     * 按年分割
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 分割后的时间段集合
     */
    private static List<DateRange> splitDateRangeByYear1(LocalDateTime startTime, LocalDateTime endTime) {
        long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
        if (seconds <= 0) {
            return new ArrayList<>();
        }
        //轮数
        long turnNum = 0;
        //分割的时间段集合,使用累加算法
        List<DateRange> dateList = new ArrayList<>();
        DateRange range = new DateRange();
        range.setBegin(startTime);
        while (true) {
            turnNum++;
            //年份相等,不再累加
            if (range.getBegin().getYear() == endTime.getYear()) {
                range.setEnd(endTime);
                range.setTurnNum(turnNum);
                dateList.add(range);
                break;
            }
           LocalDateTime tmpBegin = range.getBegin();
            startTime=tmpBegin;
            //计算出上一天的最后一秒
            LocalDateTime endDate = tmpBegin.plusYears(1).minusDays(1);
            if(endDate.getYear()==endTime.getYear()&&endDate.getMonth()!=endTime.getMonth()&&endDate.getDayOfYear()==endTime.getDayOfYear()){
                range.setEnd(endTime);
                range.setTurnNum(turnNum);
                dateList.add(range);
                break;
            }
            range.setEnd(endDate);
            range.setTurnNum(turnNum);
            dateList.add(range);
            //创建新的时间段
            range = new DateRange();
            range.setBegin(endDate.plusDays(1));
            //如果上一次结束的时间大于当前传入的开始时间跳出
            Date inendTime=Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant());
            Date newstartTime =Date.from(range.getBegin().atZone(ZoneId.systemDefault()).toInstant());
            if(isBeforeDate(newstartTime, inendTime)){
                break;
            }
        }
        return dateList;
    }

    /**
     * 按周分割
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 分割后的时间段集合
     */
    private static List<DateRange> splitDateRangeByWeek(LocalDateTime startTime, LocalDateTime endTime) {
        long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
        if (seconds <= 0) {
            return new ArrayList<>();
        }
        //轮数
        long turnNum = 0;
        //分割的时间段集合,使用累加算法
        List<DateRange> dateList = new ArrayList<>();
        DateRange range = new DateRange();
        range.setBegin(startTime);
        while (true) {
            turnNum++;
            startTime = startTime.plusWeeks(1);
            //大于截止日期时,不再累加
            if (startTime.isAfter(endTime)) {
                range.setEnd(endTime);
                range.setTurnNum(turnNum);
                dateList.add(range);
                break;
            }
            //将时间调整为该周第一天的 0时 0分 0秒
            startTime = LocalDateTime.of(LocalDate.from(startTime.with(DayOfWeek.MONDAY)), LocalTime.MIN);
            LocalDateTime tmpBegin = startTime;
            //计算出上一天的最后一秒
            LocalDateTime endDate = tmpBegin.minusSeconds(1);
            range.setEnd(endDate);
            range.setTurnNum(turnNum);
            dateList.add(range);
            //创建新的时间段
            range = new DateRange();
            range.setBegin(tmpBegin);
        }
        return dateList;
    }

    /**
     * 按日分割
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 分割后的时间段集合
     */
    private static List<DateRange> splitDateRangeByDay(LocalDateTime startTime, LocalDateTime endTime) {
        long seconds = startTime.until(endTime, ChronoUnit.SECONDS);
        if (seconds < 0) {
            return new ArrayList<>();
        }
        //轮数
        long turnNum = 0;
        //分割的时间段集合,使用累加算法
        List<DateRange> dateList = new ArrayList<>();
        DateRange range = new DateRange();
        range.setBegin(startTime);
        while (true) {
            turnNum++;
            startTime = startTime.plusDays(1);
            //大于截止日期时,不再累加
            if (startTime.isAfter(endTime)) {
                range.setEnd(endTime);
                range.setTurnNum(turnNum);
                dateList.add(range);
                break;
            }
            //将时间调整为该天的 0时 0分 0秒
            startTime = LocalDateTime.of(startTime.getYear(), startTime.getMonth(), startTime.getDayOfMonth(), 0, 0, 0);
            LocalDateTime tmpBegin = startTime;
            //计算出上一天的最后一秒
            LocalDateTime endDate = tmpBegin.minusSeconds(1);
            range.setEnd(endDate);
            range.setTurnNum(turnNum);
            dateList.add(range);
            //创建新的时间段
            range = new DateRange();
            range.setBegin(tmpBegin);
        }
        return dateList;
    }

    /**
     * 根据时间段(分割后的时间轮数,2021-10-01~2022-10-01,按月分隔时就有12轮时间段)获取时间范围
     *
     * @param dateRangeList 分隔后的时间段集合
     * @param turn          轮次,当前时间处于第几段
     * @return 时间对象
     */
    private static DateRange getRangeByTurn(List<DateRange> dateRangeList, long turn) {
        DateRange dateRange = new DateRange();
        for (DateRange d : dateRangeList) {
            if (d.getTurnNum() == turn) {
                dateRange = d;
            }
        }
        return dateRange;
    }
    /**
     *判断当前时间是否在[startTime, endTime]区间,注意三个参数的时间格式要一致
     ** @param nowTime
     *      * @param startTime
     *      * @param endTime
     *      * @return 在时间段内返回true,不在返回false
     * **/

    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    public static boolean isBeforeDate(Date nowTime,Date endTime) {
        if (nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(end);
    }

    /**
     * 月底时间处理工具类
     *
     */
    public static String downdate() throws ParseException {
     //1.获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        Date nowTime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1));
        if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
            //返回本月
            Calendar cale = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String now1 = sdf.format(new Date());
            cale.setTime(formatter.parse(now1));
            cale.add(Calendar.MONTH, 1);
            cale.set(Calendar.DAY_OF_MONTH, 0);
            String lastDayOfMonth = formatter.format(cale.getTime());
            return lastDayOfMonth;
        }else{
            //返回上月末
            Calendar cale = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String now1 = sdf.format(new Date());
            cale.setTime(formatter.parse(now1));
            cale.add(Calendar.MONTH, 0);
            cale.set(Calendar.DAY_OF_MONTH, 0);
            String lastDayOfMonth = formatter.format(cale.getTime());
            return lastDayOfMonth;
        }
     //2.判断当前时间是否本月底 是返回本月底 不是返回上月底
    }
    public static String getChinaDateFromString(String str) {
        try {
            if(str==null ){
                return getChinaCurrentDate();
            }
            str = str.trim();
            int year = 0;
            int month = 0;
            int day = 0;
            //System.out.println("==="+str);
            if(str==null || str.equals("null") || str.equals("")){
                return getChinaCurrentDate();
            }
            else if (str.indexOf("年") > 0||str.indexOf("月") > 0||str.indexOf("日") > 0) {
                return str;
            }
            else {
                if (str.length() == 10 && (str.indexOf("-") > 0)) {
                    // by meconsea  add str.indexOf("-") > 0
                    year = Integer.parseInt(str.substring(0, 4));
                    month = Integer.parseInt(str.substring(5, 7));
                    day = Integer.parseInt(str.substring(8, 10));
                }
                else if (str.length() == 8) {
                    year = Integer.parseInt(str.substring(0, 4));
                    month = Integer.parseInt(str.substring(4, 6));
                    day = Integer.parseInt(str.substring(6, 8));
                }
                else if (str.length() == 6) {
                    year = Integer.parseInt(str.substring(0, 4));
                    month = Integer.parseInt(str.substring(4, 6));
                }
                if (day == 0) {
                    str = year + "年" + month + "月";
                }
                else {
                    str = year + "年" + month + "月" + day + "日";
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
    public static String getChinaCurrentDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String str_date = dateFormat.format(Calendar.getInstance().getTime());
        str_date = str_date.substring(0, 4) + "年" + str_date.substring(4, 6) + "月" +
                str_date.substring(6, 8) + "日 ";
        return str_date;
    }
    /***
     * 获取系统当前时间
     */
    public static String getNowTime(int type) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(new Date());
        Date nowTime = sdf.parse(now);
        String now1 = sdf1.format(new Date());
        Date nowTime1 = sdf1.parse(now);
        if (type == 1) {
            // yyyy-MM-dd HH:mm:ss
            return String.valueOf(now);
        } else{
            // yyyy-MM-dd
            return String.valueOf(now1);
        }
    }

    /***
     * 字符串形式日期 补月初神操作 yyyy-MM  转yyyy-MM-dd
     * @param dateTime
     */
    public static String addDayTime(String dateTime){
        String newDateTime =null;
        if(dateTime.length()==7){
            newDateTime=dateTime+"-01";
        }
        return newDateTime;
    }
    /***
     * 统一时间格式处理工具类
     * 均统一为yyyy-MM-dd形式
     * 目前数据中存在的形式有 yyyy-MM-dd yyyy.MM.dd  yyyy/MM/dd  yyyy.MM (yyyy年MM月)暂时不处理
     */
    public static  String strToDate(String date) throws ParseException {
        String newTime =null;
        if (!StringUtils.isEmpty(date)){
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            if (date.contains("-")) {
                if(date.length()<=7){
                     newTime=DateSplitUtil.addDayTime(date);
                }else {
                    newTime=simpleDateFormat1.format(simpleDateFormat1.parse(date));;
                }
            }
            else if (date.contains(".")) {
                int count = (date.length()-date.replace(".", "").length());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM");
                SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy.MM.dd");
                if(date.length()<=7 && count == 1){
                     newTime=simpleDateFormat1.format(simpleDateFormat.parse(date));
                }else {
                    newTime=simpleDateFormat1.format(simpleDateFormat2.parse(date));
                }
            }
            else if (date.contains("/")) {
                int count = (date.length()-date.replace("/", "").length());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
                SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy/MM/dd");
                if(date.length()<=7 && count == 1){
                    newTime=simpleDateFormat1.format(simpleDateFormat.parse(date));
                }else {
                    newTime=simpleDateFormat1.format(simpleDateFormat3.parse(date));
                }
            }
        }
            return newTime;
    }


//        public static void main(String[] args) {
//            int year = 2023; // 指定年份
//            List<List<LocalDate>> workWeeks = partitionCalendar(year);
//
//            // 输出每个工作周的起始和结束日期
//            for (int i = 0; i < workWeeks.size(); i++) {
//                List<LocalDate> week = workWeeks.get(i);
//                LocalDate start = week.get(0);
//                LocalDate end = week.get(week.size() - 1);
//                System.out.println("Work Week " + (i + 1) + ": " + start + " - " + end);
//            }
//        }

        public static List<List<LocalDate>> partitionCalendar(int year) {
            List<List<LocalDate>> workWeeks = new ArrayList<>();
            int dayOfYear = Year.of(year).length(); // 获取指定年份的总天数

            LocalDate startDate = LocalDate.of(year, Month.JANUARY, 1);
            LocalDate endDate = LocalDate.of(year, Month.DECEMBER, 31);

            // 划分工作周
            List<LocalDate> week = new ArrayList<>();
            for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
                week.add(date);

                // 判断是否为工作周的最后一天
                if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfYear() == dayOfYear) {
                    workWeeks.add(week);
                    week = new ArrayList<>();
                }
            }

            return workWeeks;
        }

//    public static void main(String[] args) {
//        int year = 2023; // 指定年份
//        int month = 9; // 指定月份
//        List<WorkDay> workDays = partitionCalendar(year, month);
//
//        // 输出每个工作日是本月的第几周
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");
//        for (WorkDay workDay : workDays) {
//            LocalDate date = workDay.getDate();
//            String week = workDay.getWeek();
//            System.out.println(formatter.format(date) + " is in Week " + week);
//        }
//    }

    public static List<WorkDay> partitionCalendar(int year, int month) {
        List<WorkDay> workDays = new ArrayList<>();
        YearMonth yearMonth = YearMonth.of(year, month);

        LocalDate startDate = yearMonth.atDay(1);
        LocalDate endDate = yearMonth.atEndOfMonth();

        // 划分工作日
        for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
            DayOfWeek dayOfWeek = date.getDayOfWeek();

            // 判断是否为工作日(周一到周五)
            if (dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) {
                int week = date.get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfMonth()); // 获取是本月的第几周
                workDays.add(new WorkDay(date, String.valueOf(week)));
            }
        }

        return workDays;
    }

    static class WorkDay {
        private LocalDate date;
        private String week;

        public WorkDay(LocalDate date, String week) {
            this.date = date;
            this.week = week;
        }

        public LocalDate getDate() {
            return date;
        }

        public String getWeek() {
            return week;
        }
    }

//    public static void main(String[] args) {
//        int year = 2022; // 指定年份
//        int month = 3; // 指定月份
//        List<Week> weeklyCalendar = generateWeeklyCalendar(year, month);
//
//        // 输出周日历
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");
//        for (Week week : weeklyCalendar) {
//            System.out.println("Week " + week.getWeekNumber() + ":");
//            for (LocalDate date : week.getDates()) {
//                System.out.print(formatter.format(date) + " ");
//            }
//            System.out.println();
//        }
//    }

    public static List<Week> generateWeeklyCalendar(int year, int month) {
        List<Week> weeklyCalendar = new ArrayList<>();
        YearMonth yearMonth = YearMonth.of(year, month);

        LocalDate startDate = yearMonth.atDay(1);
        LocalDate endDate = yearMonth.atEndOfMonth();

        int weekNumber = 1;
        List<LocalDate> dates = new ArrayList<>();
        for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
            dates.add(date);

            // 如果是周日,则表示一个完整的周结束,创建一个新的周
            if (date.getDayOfWeek() == DayOfWeek.SUNDAY) {
                weeklyCalendar.add(new Week(weekNumber, new ArrayList<>(dates)));
                dates.clear();
                weekNumber++;
            }
        }

        // 处理最后一周不满一周的情况
        if (!dates.isEmpty()) {
            weeklyCalendar.add(new Week(weekNumber, dates));
        }

        return weeklyCalendar;
    }

//    static class Week {
//        private int weekNumber;
//        private List<LocalDate> dates;
//
//        public Week(int weekNumber, List<LocalDate> dates) {
//            this.weekNumber = weekNumber;
//            this.dates = dates;
//        }
//
//        public int getWeekNumber() {
//            return weekNumber;
//        }
//
//        public List<LocalDate> getDates() {
//            return dates;
//        }
//    }

    public static void main(String[] args) {
        int year = 2023; // 指定年份
        Map<Integer, List<Week>> yearlyCalendar = generateYearlyCalendar(year);

        // 输出每个月的周日历
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int month : yearlyCalendar.keySet()) {
            System.out.println("Month " + month + ":");
            List<Week> weeklyCalendar = yearlyCalendar.get(month);
            for (Week week : weeklyCalendar) {
                System.out.println("  Week " + week.getWeekNumber() + ":");
                //判断weeklyCalendar.get(0).size是否等于7  不等于的话从上一期上不全
                // 1.不是1月的话拿上一个月的最后一月的最后一星期的数据顺序追加到最前面
                // 2.1月份的话需要拿上一年最后一月最后一星期的数据来补全

                //判断weeklyCalendar.get((weeklyCalendar.size))不足七天的话舍弃
                int last = weeklyCalendar.size()-1;
//                if(weeklyCalendar.get(last).dates.size() < 7){
//                    weeklyCalendar.remove(last);
//                }
                for (LocalDate date : week.getDates()) {
                    System.out.print("    " + formatter.format(date) + " ");
                }
                System.out.println();
            }
        }
    }

    public static Map<Integer, List<Week>> generateYearlyCalendar(int year) {
        Map<Integer, List<Week>> yearlyCalendar = new TreeMap<>();

        for (int month = 1; month <= 12; month++) {
            YearMonth yearMonth = YearMonth.of(year, month);

            LocalDate startDate = yearMonth.atDay(1);
            LocalDate endDate = yearMonth.atEndOfMonth();

            int weekNumber = 1;
            List<Week> weeklyCalendar = new ArrayList<>();
            List<LocalDate> dates = new ArrayList<>();
            for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
                dates.add(date);

                // 如果是周日,则表示一个完整的周结束,创建一个新的周
                if (date.getDayOfWeek() == DayOfWeek.SUNDAY) {
                    weeklyCalendar.add(new Week(weekNumber, new ArrayList<>(dates)));
                    dates.clear();
                    weekNumber++;
                }
            }

            // 处理最后一周不满一周的情况
            if (!dates.isEmpty()) {
                weeklyCalendar.add(new Week(weekNumber, dates));
            }

            yearlyCalendar.put(month, weeklyCalendar);
        }

        return yearlyCalendar;
    }

    static class Week {
        private int weekNumber;
        private List<LocalDate> dates;

        public Week(int weekNumber, List<LocalDate> dates) {
            this.weekNumber = weekNumber;
            this.dates = dates;
        }

        public int getWeekNumber() {
            return weekNumber;
        }

        public List<LocalDate> getDates() {
            return dates;
        }
    }
//    public static void main(String[] args) throws ParseException {
//        String a="2022-6-1";
//        String b="2017.3";
//        String c="2021.11.1";
//        String d="2021.1.1";
//        String e="2020/11/1";
//        String f="2020/7";
//        String newtime=DateSplitUtil.strToDate(a);
//        System.out.println("时间:"+newtime);
//         newtime=DateSplitUtil.strToDate(b);
//        System.out.println("时间:"+newtime);
//         newtime=DateSplitUtil.strToDate(c);
//        System.out.println("时间:"+newtime);
//         newtime=DateSplitUtil.strToDate(d);
//        System.out.println("时间:"+newtime);
//         newtime=DateSplitUtil.strToDate(e);
//        System.out.println("时间:"+newtime);
//         newtime=DateSplitUtil.strToDate(f);
//        System.out.println("时间:"+newtime);
//
//    }
//    public static void main(String[] args) {
//        String s ="2023-01-11";
//        String newtime=DateSplitUtil.addDayTime(s);
//        System.out.println("字符串的长度为:"+s.length());
//        System.out.println("新时间:"+newtime);
//    }
//    public static void main(String[] args) throws ParseException {
//       String s =DateSplitUtil.getNowTime(1);
//       System.out.println(s);
//    }
//    public static void main(String[] args) {
//        /**
//         * 判断当前时间是否在一个时间段内 HH:mm 格式
//         */
//        String strStartTime1 = "2024-1-1";
//        String strEndTime1 = "2025-12-31";
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String now = sdf.format(new Date());
//        Date nowTime;
//        try {
//            nowTime = sdf.parse(now);
//            Date startTime1 = sdf.parse(strStartTime1);
//            Date endTime1 = sdf.parse(strEndTime1);
//            // 注:也可以通过此方式判断当前时间是否具体时间段内 yyyy-MM-dd HH:mm:ss格式 [2022-03-09 12:00:00,2022-03-10 15:00:00]
//            //   当前时间和时间段的格式保持一致即可判断
//            if (isBeforeDate(nowTime, startTime1)) {
//                System.out.println("当前时间在时间段内[" + isBeforeDate(nowTime, startTime1) + ",");
//            } else {
//                System.out.println("当前时间不再时间段内[" + isBeforeDate(nowTime, startTime1) + "]");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//       public static void main(String[] args) throws ParseException {
//           String s=DateSplitUtil.downdate();
//           String s1=DateSplitUtil.getChinaDateFromString(DateSplitUtil.downdate());
//           System.out.println("结果输出"+DateSplitUtil.getChinaDateFromString(DateSplitUtil.downdate()));
//        //2019-10-25 13:25:55
//        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
//        Date start = dateFormat1.parse("2020-08-01");
//        Date end = dateFormat1.parse("2025-08-05");
//        LocalDateTime startTime = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
//        LocalDateTime endTime = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
//        //LocalDateTime startTime = LocalDateTime.of(2019, 10, 25, 13, 25, 55);
//        //2020-01-25 12:30:32
//        //LocalDateTime endTime = LocalDateTime.of(2020, 1, 25, 12, 30, 32);
//        List<DateRange> dateMonthRanges = DateSplitUtil.splitDateRangeByYear1(startTime, endTime);
//        System.out.println("按月切分后的结果:\n{}"+dateMonthRanges);
//        System.out.println("按月切分后的结果:\n{}"+dateMonthRanges.size());
//    }
}
 

下面是运行的效果截图:

相关文章:

【知识分享】Java获取全年每个月的有几周且每周是几号到几号

加哥本周给大家分享一期怎么用java把全年每个月有几周&#xff0c;本周是几号到几号的工具类。便于大家根据需求获取想要的形式进行改造。话不多说&#xff0c;直接给大家上代码。 package com.techfantasy.common.utils; import com.techfantasy.common.entity.DateRange; i…...

学信息系统项目管理师第4版系列11_信息安全管理

1. 信息安全基础 1.1. 保密性(Confidentiality&#xff09; 1.1.1. 信息不被未授权者知晓的属性 1.1.2. 确保信息不暴露给未授权的实体或进程 1.2. 完整性(Integrity) 1.2.1. 信息是正确的、真实的、未被篡改的、完整无缺的属性 1.2.2. 只有得到允许的人才能修改数据&…...

sql注入原理分析

...

Mac磁盘空间满了怎么办?Mac如何清理磁盘空间

你是不是发现你的Mac电脑存储越来越满&#xff0c;甚至操作系统本身就占了100多G的空间&#xff1f;这不仅影响了电脑的性能&#xff0c;而且也让你无法存储更多的重要文件和软件。别担心&#xff0c;今天这篇文章将告诉你如何清除多余的文件&#xff0c;让你的Mac重获新生。 一…...

能ping通但无法上网的问题

大家好&#xff0c;今天我要和大家分享一下当你的IP地址能够成功 ping 通&#xff0c;却无法上网时该如何解决这个问题。这是一个相当常见的情况&#xff0c;在网络故障排查中经常遇到。别担心&#xff0c;我将为你揭开这个谜题&#xff0c;提供一些解决方案和技巧。 首先&…...

仿制 Google Chrome 的恐龙小游戏

通过仿制 Google Chrome 的恐龙小游戏&#xff0c;我们可以掌握如下知识点&#xff1a; 灵活使用视口单位掌握绝对定位JavaScript 来操作 CSS 变量requestAnimationFrame 函数的使用无缝动画实现 页面结构 实现页面结构 通过上述的页面结构我们可以知道&#xff0c;此游戏中…...

Redis面试题(五)

文章目录 前言一、使用过 Redis 做异步队列么&#xff0c;你是怎么用的&#xff1f;有什么缺点&#xff1f;二、 什么是缓存穿透&#xff1f;如何避免&#xff1f;什么是缓存雪崩&#xff1f;何如避免&#xff1f;总结 前言 使用过 Redis 做异步队列么&#xff0c;你是怎么用的…...

组队竞赛(int溢出问题)

目录 一、题目 二、代码 &#xff08;一&#xff09;没有注意int溢出 &#xff08;二&#xff09;正确代码 1. long long sum0 2. #define int long long 3. 使用现成的sort函数 一、题目 二、代码 &#xff08;一&#xff09;没有注意int溢出 #include <iostream&g…...

Swift SwiftUI 隐藏键盘

如果仅支持 iOS 15 及更高版本&#xff0c;则可以通过聚焦和取消聚焦来激活和关闭文本字段的键盘。 在最简单的形式中&#xff0c;这是使用 FocusState 属性包装器和 focusable() 修饰符完成的-第一个存储一个布尔值&#xff0c;用于跟踪第二个当前是否被聚焦。 Code struct C…...

Python与数据分析--Pandas-1

目录 1.Pandas简介 2.Series的创建 1.通过数组列表来创建 2.通过传入标量创建 3.通过字典类型来创建 4.通过numpy来创建 3.Series的索引和应用 1. 通过index和values信息 2. 通过切片方法获取信息 4.DataFrame的创建 1.直接创建 2.矩阵方式创建 3.字典类型创建 5.…...

如何完美通过token获取用户信息(springboot)

1. 什么是Token&#xff1f; 身份验证令牌&#xff08;Authentication Token&#xff09;&#xff1a;在身份验证过程中&#xff0c;“token” 可以表示一个包含用户身份信息的令牌。 例如 Token&#xff08;JWT&#xff09;是一种常见的身份验证令牌&#xff0c;它包含用户的…...

2023 “华为杯” 中国研究生数学建模竞赛(B题)深度剖析|数学建模完整代码+建模过程全解全析

华为杯数学建模B题 当大家面临着复杂的数学建模问题时&#xff0c;你是否曾经感到茫然无措&#xff1f;作为2021年美国大学生数学建模比赛的O奖得主&#xff0c;我为大家提供了一套优秀的解题思路&#xff0c;让你轻松应对各种难题。 让我们来看看研赛的B题呀~&#xff01; 问…...

文件相关工具类

文章目录 1.MultipartFile文件转File2.读取文件(txt、json)3.下载网络文件4.压缩文件 1.MultipartFile文件转File public File transferToFile(MultipartFile multipartFile) { // 选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()…...

18795-2012 茶叶标准样品制备技术条件

声明 本文是学习GB-T 18795-2012 茶叶标准样品制备技术条件. 而整理的学习笔记,分享出来希望更多人受益,如果存在侵权请及时联系我们 1 范围 本标准规定了各类茶叶(除再加工茶)标准样品的制备、包装、标签、标识、证书和有效期。 本标准适用于各类茶叶(除再加工茶)感官品质…...

C++11互斥锁的使用

是C11标准库中用于多线程同步的库&#xff0c;提供互斥锁(mutex)及其相关函数。 以下是一些基本的使用示例&#xff1a; 1.创建和销毁互斥锁 #include <mutex>std::mutex mtx;2.加锁 std::lock_guard<std::mutex> lock(mtx); // 加锁 // 或者 mtx.lock(); //…...

unity 桌面程序

using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; public class chuantou : MonoBehaviour { [DllImport(“user32.dll”)] public static extern int MessageBox(IntPtr hwnd,string t…...

echarts统一纵坐标y轴的刻度线,刻度线对齐。

要求&#xff1a; 纵坐标刻度线对齐&#xff1b;刻度间隔为5&#xff1b;去掉千位默认的逗号&#xff1b;刻度最小是0. 效果图&#xff1a; 代码&#xff1a; yAxis: [{type: "value",position: "left",name: "kW",offset: 100,nameTextStyle:…...

一个数据库版本兼容问题

mysql旧的版本号是&#xff1a;5.3.10 本机版本号是&#xff1a;8.0.22 报错&#xff1a;“com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create” 1.程序里做兼容&#xff1a; <dependency><groupId>mysql</groupId>…...

学习Nano编辑器:入门指南、安装步骤、基本操作和高级功能

文章目录 使用Nano编辑器入门指南引言1.1 关于Nano编辑器1.2 Nano的起源和特点 安装Nano2.1 在Debian/Ubuntu系统上安装Nano2.2 在CentOS/RHEL系统上安装Nano2.3 在其他Linux发行版上安装Nano 启动Nano3.1 命令行启动Nano3.2 打开文件 Nano的基本操作4.1 光标移动和选择文本4.2…...

在北京多有钱能称为富

背景 首先声明&#xff0c;此讨论仅限个人的观点&#xff0c;因为我本身不富嘛&#xff0c;所以想法应该非常局限。 举个栗子 富二代问我朋友&#xff0c;100~1000w之间&#xff0c;推荐一款车&#xff1f; 一开始听到这个问题的时候&#xff0c;有被唬住&#xff0c;觉得预…...

Chrome扩展程序开发随记

在Chrome浏览器向正被浏览的外网网页植入自定义JS脚本 为了实现如标题的目的&#xff0c;需要开发一个Chrome扩展程序。接下来内容是实现简要步骤&#xff1a; 一、新建文件夹&#xff0c;命名为项目名&#xff0c;如“MyPlugin”。 二、进入文件夹内&#xff0c;新建名为“…...

使用命令行快速创建Vite项目

一、构建项目 在终端中使用如下命令行&#xff1a; npm create vite 二、定义项目名称 三、选择项目类型 Vanilla是我们常用的JavaScript&#xff0c;Vue和React是常用前端框架&#xff0c;可以根据自己的需要进行选择 通过上下键进行选择&#xff0c;按下回车进行确认 创建…...

int *a, int **a, int a[], int *a[]的区别

int *a; ---定义一个指向整型变量的指针a int **a; ---定义一个指向整型变量指针的指针a int a[]; ---定义一个整型变量数组a int *a[]; ---定义一个指向整型变量指针的数组a...

leetcode100----双指针

283. 移动零 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数组进行操作。 示例 1:输入: nums [0,1,0,3,12] 输出: [1,3,12,0,0] 示例 2:输入: nums …...

ORM基本操作

ORM基本操作 基本操作包括增删改查操作&#xff0c;即(CRUD操作) CRUD是指在做计算处理时的增加(Create)、读取查询(Read)、更新Update)和删除(Delete) ORM CRUD 核心-> 模型类管理器对象 每个继承自 models.Model 的模型类&#xff0c;都会有一个 objects 对象被同样继…...

c语言进阶部分详解(指针进阶2)

大家好&#xff01;我快马加鞭接着写出指针部分part2部分。第一部分见&#xff1a;c语言进阶部分详解&#xff08;指针进阶1&#xff09;_总之就是非常唔姆的博客-CSDN博客 指针初阶部分见&#xff1a;c语言进阶部分详解&#xff08;指针初阶&#xff09;_总之就是非常唔姆的博…...

Java基础(一)——Hello World,8种数据类型,键盘录入

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1f4c3;个人状态&#xff1a; 研发工程师&#xff0c;现效力于中国工业软件事业 &#x1f680;人生格言&#xff1a; 积跬步…...

JAVA学习笔记(IF判断结构)

/*** 学习JAVA单分支结构* 掷骰子游戏* 1.如果三个随机数的和大于15&#xff0c;则手气不错&#xff1b;* 2.如果三个随机数的和在10-15之间&#xff0c;手气一般&#xff1b;* 3.如果三个随机数的和1在0以下&#xff0c;手气不太好。*/public class TestIf01 {public static v…...

【跟小嘉学 PHP 程序设计】二、PHP 基本语法

系列文章目录 【跟小嘉学 PHP 程序设计】一、PHP 开发环境搭建 【跟小嘉学 PHP 程序设计】二、PHP 基本语法 文章目录 系列文章目录@[TOC](文章目录)前言一、PHP基本语法1.1、hello,world1.2、PHP语法的其他形式1.2.1、简短形式(了解即可)1.2.2、Script 风格1.2.3、ASP 风格1…...

面试总结之微服务篇

一、概览 1、微服务常用组件 微服务给系统开发带来了诸多问题和挑战&#xff0c;如服务间通信和调用的复杂性、数据一致性和事务管理、服务治理和版本管理等&#xff0c;为解决应对这些问题和挑战&#xff0c;各种微服务组件应运而生微服务的常见组件和实现&#xff1a; 1…...

黑龙江企业网站建设公司/网络营销课程大概学什么内容

生产环境中微服务的发布是非常频繁的&#xff0c;对于一些互联网型的项目&#xff0c;甚至在你与他人谈话闲聊的几分钟内便有新的版本发布出来&#xff1b;一般的&#xff0c;每发布一个新的微服务&#xff0c;网关就可能需要为新发布的微服务定义对应的访问路由&#xff0c;如…...

帮别人做时时彩网站/免费建自己的网址

华为防火墙USG5500重点&#xff1a;什么是防火墙&#xff1b;防火墙基础&#xff1b;防火墙功能配置一.什么是防火墙&#xff1a;1.什么是防火墙&#xff1a;防火墙主要用于保护一个网络免受来自另一个网络的***和***行为&#xff0c;因其隔离、防守属性&#xff0c;防火墙灵活…...

佛山电子商务网站建设/最近新闻大事件

最近后台读者说自己最近在疯狂投简历&#xff0c;有的石沉大海&#xff0c;但还好不是全军覆没。前两天好不容易熬到了阿里的四面&#xff0c;跟我聊了一下&#xff0c;面试官拿哪些题为难了他&#xff1f;前面几题还好&#xff0c;问的是有关JVM的一些问题&#xff0c;比如说J…...

重庆建设网站哪个好/广告投放平台有哪些

题面 传送门 题解 以下记\(S_i\{1,2,3,...,i\}\) 我们先用凸包旋转卡壳求出直径的长度&#xff0c;并记直径的两个端点为\(i,j\)&#xff08;如果有多条直径随机取两个端点&#xff09; 因为这个序列被\(random\_shuffle\)过&#xff0c;有\(E(\max(i,j))O({2\over 3}n)\)&…...

个人怎么做ipv6的网站/百度收录推广

首先保证没有客户端使用并在administrator管理器的工具中锁定数据库&#xff0c;其次把整个数据库备份一份&#xff0c;以防万一。1、在VSS服务器上运行CMD&#xff0c;打开DOS窗口。2、进入VSS安装目录。如 cd C:\Program Files (x86)\Microsoft Visual SourceSafe3、运行修复…...

wordpress中途修改固定连接/百度下载安装到手机

前端的三把利器&#xff1a; HTML&#xff1a;一个人 CSS&#xff1a;这个人的衣服 JS&#xff1a;这个人的行为 HTML&#xff08;超文本标记语言&#xff09; html代码实际上就是一套能够被浏览器所识别的规则代码&#xff0c;由一个个标签组成。html代码就是一大长串字符串&a…...