项目中需要按月份把时间段按月份拆开为每个月一个时间段用作统计
参考自:Java将日期拆分成按周或月时间段统计 - 暖然 - 博客园 在此基础上修改后使用.
public static ListrangeMonthDate(Date sDate, Date eDate){ List list = new ArrayList<>(); Calendar sCalendar = Calendar.getInstance(); sCalendar.setFirstDayOfWeek(Calendar.MONDAY); sCalendar.setTime(sDate); Calendar eCalendar = Calendar.getInstance(); eCalendar.setFirstDayOfWeek(Calendar.MONDAY); eCalendar.setTime(eDate); boolean bool =true; while (sCalendar.getTime().getTime() <= eCalendar.getTime().getTime()) { Date[] dates = getLastDateArr(list); if (bool || sCalendar.get(Calendar.DAY_OF_MONTH) == 1) { dates[0] = sCalendar.getTime(); bool = false; } if (sCalendar.get(Calendar.DAY_OF_MONTH) == sCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)) { dates[1] = sCalendar.getTime(); } sCalendar.add(Calendar.DAY_OF_MONTH, 1); } Date[] dates = list.get(list.size() - 1); if (dates[1] == null) { dates[1] = eCalendar.getTime(); } return list; } private static Date[] getLastDateArr(List dateList) { if (dateList.size() == 0 || dateList.get(dateList.size() - 1)[1] != null) { Date[] temp = new Date[2]; dateList.add(temp); return temp; } return dateList.get(dateList.size() - 1); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)