java按照月份把一个时间段拆分成多个时间区间并返回相差天数

代码实现:

package com.example.demo.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 将时间段按照月份拆分
 * @author qzz
 * @date 2023/5/18
 */
public class SplitDateUtil {
    /**
     * 根据一段时间区间,按月份拆分成多个时间段
     * @param startDate 开始日期
     * @param endDate  结束日期
     * @return
     */
    public static List<KeyValueForDate> getKeyValueForDate(String startDate,String endDate) {
        List<KeyValueForDate> list = null;
        try {
            list = new ArrayList<KeyValueForDate>();
            String firstDay = "";
            String lastDay = "";
            // 定义起始日期
            Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
            // 定义结束日期
            Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
            // 定义日期实例
            Calendar dd = Calendar.getInstance();
            // 设置日期起始时间
            dd.setTime(d1);
            Calendar cale = Calendar.getInstance();
            Calendar c = Calendar.getInstance();
            c.setTime(d2);
            int startDay = d1.getDate();
            int endDay = d2.getDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            KeyValueForDate keyValueForDate = null;
            while (dd.getTime().before(d2)) {
                // 判断是否到结束日期
                keyValueForDate = new KeyValueForDate();
                cale.setTime(dd.getTime());

                if(dd.getTime().equals(d1)){
                    cale.set(Calendar.DAY_OF_MONTH, dd
                            .getActualMaximum(Calendar.DAY_OF_MONTH));
                    lastDay = sdf.format(cale.getTime());
                    keyValueForDate.setStartDate(sdf.format(d1));
                    keyValueForDate.setEndDate(lastDay);
                    keyValueForDate.setDays(differentDays(d1,sdf.parse(lastDay)));

                }else if(dd.get(Calendar.MONTH) == d2.getMonth() && dd.get(Calendar.YEAR) == c.get(Calendar.YEAR)){
                    cale.set(Calendar.DAY_OF_MONTH,1);
                    //取第一天
                    firstDay = sdf.format(cale.getTime());

                    keyValueForDate.setStartDate(firstDay);
                    keyValueForDate.setEndDate(sdf.format(d2));
                    keyValueForDate.setDays(differentDays(sdf.parse(firstDay),d2));
                }else {
                    cale.set(Calendar.DAY_OF_MONTH,1);
                    //取第一天
                    firstDay = sdf.format(cale.getTime());

                    cale.set(Calendar.DAY_OF_MONTH, dd
                            .getActualMaximum(Calendar.DAY_OF_MONTH));
                    lastDay = sdf.format(cale.getTime());

                    keyValueForDate.setStartDate(firstDay);
                    keyValueForDate.setEndDate(lastDay);
                    keyValueForDate.setDays(differentDays(sdf.parse(firstDay),sdf.parse(lastDay)));

                }
                list.add(keyValueForDate);
                // 进行当前日期月份加1
                dd.add(Calendar.MONTH, 1);
            }
            if(endDay<startDay){
                keyValueForDate = new KeyValueForDate();

                cale.setTime(d2);
                cale.set(Calendar.DAY_OF_MONTH,1);
                //取第一天
                firstDay = sdf.format(cale.getTime());

                keyValueForDate.setStartDate(firstDay);
                keyValueForDate.setEndDate(sdf.format(d2));
                keyValueForDate.setDays(differentDays(sdf.parse(firstDay),d2));
                list.add(keyValueForDate);
            }
        } catch (Exception e) {
            return null;
        }
        return list;
    }

    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            /*+1包含当天*/
            return timeDistance + (day2-day1+1) ;
        }
        else    //同一年
        {
            /*+1包含当天*/
            return day2-day1+1;
        }
    }

    public static class KeyValueForDate {
        private String startDate;
        private String endDate;
        private Integer days;
        public String getStartDate() {
            return startDate;
        }
        public void setStartDate(String startDate) {
            this.startDate = startDate;
        }
        public String getEndDate() {
            return endDate;
        }
        public void setEndDate(String endDate) {
            this.endDate = endDate;
        }

        public Integer getDays() {
            return days;
        }

        public void setDays(Integer days) {
            this.days = days;
        }
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月",Locale.CHINA);
        //根据一段时间区间,按月份拆分成多个时间段
        String minDate = "2023-01-01";
        String maxDate = "2023-05-01";
        List<KeyValueForDate> list = SplitDateUtil1.getKeyValueForDate(minDate,maxDate);
        System.out.println("月报---------------开始日期--------------结束日期--------------相差天数");
        for(KeyValueForDate date : list){
            String name = sdf.format(formatter.parse(date.getStartDate()))+"月报";
            System.out.println(name+"-----"+date.getStartDate()+"-----------"+date.getEndDate()+"-----------"+date.getDays());
        }
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐