/*** 获取两日期相差天数
*
*@parambeginDateStr 时间起点
*@paramendDateStr 时间终点
*@paramTimeType 时间类型 yyyy-MM-dd || yyyyMMdd || ....
*@returnlong /天数*/
public static longgetDaySub(String beginDateStr, String endDateStr, String TimeType) {long day = 0;
SimpleDateFormat format;try{
format= newSimpleDateFormat(TimeType);
}catch(Exception e) {
System.out.println("getDaySub——>TimeType:"+TimeType);
format= new SimpleDateFormat("yyyy-MM-dd");
}
Date beginDate;
Date endDate;try{
beginDate=format.parse(beginDateStr);
endDate=format.parse(endDateStr);
day= (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
}catch(Exception e) {
System.out.println("获取两时间差值出现异常 begin:" + beginDateStr + " end:" +endDateStr);
}if (day < 0) {
day= day * -1;
}returnday;
}/*** 将一段时间分割为两个连续的时间段
*
*@paramdp 时间集合
*@paramTimeType 时间类型 yyyy-MM-dd || yyyyMMdd || ....
*@return1.两个相连的时间集合 2.null*/
public static List> cutDatePair(Pairdp, String TimeType) {
List> lst = new ArrayList<>();long ts =getDaySub(dp.left(), dp.right(), TimeType);if (ts < 2) {return null;
}int day = (int) ts / 2;
SimpleDateFormat format;try{
format= newSimpleDateFormat(TimeType);
}catch(Exception e) {
format= new SimpleDateFormat("yyyy-MM-dd");
}
Date beginDate;try{
beginDate=format.parse(dp.left());
Calendar beginDataEnd=Calendar.getInstance();
beginDataEnd.setTime(beginDate);
beginDataEnd.add(Calendar.DAY_OF_YEAR, day);//"开始"的结束时间
Calendar endDataBegin=Calendar.getInstance();
endDataBegin.setTime(beginDate);
endDataBegin.add(Calendar.DAY_OF_YEAR, day+ 1);//"结束"的开始时间
lst.add(new Pair<>(dp.left(), format.format(beginDataEnd.getTime())));
lst.add(new Pair<>(format.format(endDataBegin.getTime()), dp.right()));
System.out.println("拆分时间 " + dp.left() + "-" + dp.right() + " -->> " + dp.left() + "-" +format
.format(beginDataEnd.getTime())+
" and " + format.format(endDataBegin.getTime()) + "-" +dp.right());
}catch(Exception e) {
}if(lst.isEmpty() || lst.size()!=2){return null;
}returnlst;
}