package com.mw.web.common.utils;
import com.mw.web.param.KeyValueForDate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
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);
dd.add(Calendar.MONTH, 1);// 进行当前日期月份加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 (ParseException 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 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[]){
String minDate = "2020-12-27";
String maxDate = "2021-05-01";
List<KeyValueForDate> list = SplitDateUtil.getKeyValueForDate(minDate,maxDate);
System.out.println("开始日期--------------结束日期--------------相差天数");
for(KeyValueForDate date : list){
System.out.println(date.getStartDate()+"-----"+date.getEndDate()+"-----"+date.getDays());
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?