/**
* 自然月拆分
* @param beginDate 开始时间
* @return
* beginDate 开始时间
* endDate 本月结束时间
* preDate 下月开始时间
* dateNum 开始及结束时间之间的天数
* mothNum 当月天数
*/
public static ExtendMap naturalMonthSplit(Date beginDate){
Date preDate = DateUtils.ceiling(beginDate, Calendar.MARCH) ; // 2018-04-01
Date endDate = DateUtils.addDays(preDate, -1) ; // 2018-03-31
long dateNum = DataTimeUtils.differentDate("DAY", beginDate, endDate)+ 1 ; //26
long mothNum = DataTimeUtils.differentDate("DAY", getPreMonthDate(beginDate), endDate)+1 ; //31
ExtendMap returnMap = new ExtendMap<>();
returnMap.put("beginDate",beginDate);
returnMap.put("endDate",endDate);
returnMap.put("preDate", preDate);
returnMap.put("dateNum", dateNum);
returnMap.put("mothNum", mothNum);
return returnMap ;
}
/**
* 自然月拆分
* 获取开始时间到结束时间中每个月的时间及天数
* @param beginDate 开始时间
* @param endDate 结束时间
* @return
*/
public static List> naturalMonthSplit(Date beginDate , Date endDate){
List> returnList = new ArrayList<>();
long num = DataTimeUtils.differentDate("M", beginDate, endDate);
for(long i=0; i<=num ; i++) {
ExtendMap map = new ExtendMap<>();
map = naturalMonthSplit(beginDate);
if(i == num){
map.put("endDate", endDate);
map.put("dateNum", DataTimeUtils.differentDate("D", (Date) map.get("beginDate"), endDate)+1);
}
returnList.add(map);
beginDate = (Date) map.get("preDate");
}
return returnList ;
}
/**
* 合同月拆分
* @param beginDate
* @return
*/
public static ExtendMap contractMonthSplit(Date beginDate) {
ExtendMap map = new ExtendMap<>();
Calendar cal1 = Calendar.getInstance();
cal1.setTime(beginDate);
int day = cal1.get(Calendar.DAY_OF_MONTH);
Date afterDate;
if(day > 27 ) {
Date endDate = DateUtils.addMonths(beginDate, 1);
Date beginDateTemp =DateUtils.addDays(getPreMonthDate(endDate),-1) ;
int dayEndNum = (int) DataTimeUtils.differentDate("D", beginDate, beginDateTemp) + 1 ;
afterDate= DateUtils.addDays(getEndMonthDate(endDate), -dayEndNum);
map.put("beginDate", beginDate);
map.put("endDate", afterDate);
}else {
afterDate = DateUtils.addDays( DateUtils.addMonths(beginDate, 1),-1);
map.put("beginDate", beginDate);
map.put("endDate", afterDate);
}
long dateNum = DataTimeUtils.differentDate("DAY", beginDate, afterDate)+ 1 ; //26
long mothNum = DataTimeUtils.differentDate("DAY", beginDate, afterDate)+ 1 ;
map.put("dateNum", dateNum);
map.put("mothNum", mothNum);
return map ;
}
/**
* 合同月拆分
* @param beginDate
* @param endDate
* @return
*/
public static List> contractMonthSplit(Date beginDate , Date endDate) {
List> returnList = new ArrayList<>();
long num = DataTimeUtils.differentDate("M", beginDate, endDate);
Date tempDate = beginDate ;
for(int i=0;i
ExtendMap map =contractMonthSplit(tempDate);
tempDate = (Date) map.get("endDate");
tempDate = DateUtils.addDays(tempDate, 1);
if(i == num){
map.put("endDate", endDate);
map.put("dateNum", DataTimeUtils.differentDate("D", (Date) map.get("beginDate"), endDate)+1);
}
returnList.add(map);
}
return returnList ;
}
/**
* 日期拆分调用
* @param beginDate
* @param endDate
* @param type
* @return
*/
public static List> dateSplit(Date beginDate , Date endDate , Long type ) {
if(type == 1) {
return naturalMonthSplit(beginDate, endDate);
}else{
return contractMonthSplit(beginDate, endDate);
}
}
转载于:https://www.cnblogs.com/gemiaomiao/p/10550746.html