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());
}
}