|
@@ -0,0 +1,80 @@
|
|
|
+package com.management.platform.util;
|
|
|
+
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.ehcache.core.internal.util.CollectionUtil;
|
|
|
+
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class SplitDateUtil {
|
|
|
+ /**
|
|
|
+ * 根据传入的参数,来对日期区间进行拆分,返回拆分后的日期List
|
|
|
+ * @param statisticsType
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ * @author lihq 2019-6-24
|
|
|
+ * @editor
|
|
|
+ * @editcont
|
|
|
+ */
|
|
|
+ public static List<String> doDateByStatisticsType(String statisticsType,String startDate,String endDate) throws ParseException {
|
|
|
+ List<String> listWeekOrMonth = new ArrayList<String>();
|
|
|
+ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date sDate = dateFormat.parse(startDate);
|
|
|
+ Calendar sCalendar = Calendar.getInstance();
|
|
|
+ sCalendar.setFirstDayOfWeek(Calendar.MONDAY);
|
|
|
+ sCalendar.setTime(sDate);
|
|
|
+ Date eDate = dateFormat.parse(endDate);
|
|
|
+ Calendar eCalendar = Calendar.getInstance();
|
|
|
+ eCalendar.setFirstDayOfWeek(Calendar.MONDAY);
|
|
|
+ eCalendar.setTime(eDate);
|
|
|
+ boolean bool =true;
|
|
|
+ if(statisticsType.equals("week")){
|
|
|
+ while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
|
|
|
+ if(bool||sCalendar.get(Calendar.DAY_OF_WEEK)==2||sCalendar.get(Calendar.DAY_OF_WEEK)==1){
|
|
|
+ listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
|
|
|
+ bool = false;
|
|
|
+ }
|
|
|
+ sCalendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
|
|
|
+ if(bool||sCalendar.get(Calendar.DAY_OF_MONTH)==1||sCalendar.get(Calendar.DAY_OF_MONTH)==sCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)){
|
|
|
+ listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
|
|
|
+ bool = false;
|
|
|
+ }
|
|
|
+ sCalendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
|
|
|
+ if(listWeekOrMonth.size()%2!=0){
|
|
|
+ listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
|
|
|
+ }
|
|
|
+ return listWeekOrMonth;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ List<String> list = doDateByStatisticsType("month", "2022-01-01", "2022-03-30");
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ String sDate = list.get(i);
|
|
|
+ String eDate = list.get(i + 1);
|
|
|
+ i++;
|
|
|
+ System.out.println(sDate+"-----"+eDate);
|
|
|
+ List<String> twoList = doDateByStatisticsType("week", sDate, eDate);
|
|
|
+ for (int i1 = 0; i1 < twoList.size(); i1++) {
|
|
|
+ String sDate1 = twoList.get(i1);
|
|
|
+ String eDate1 = twoList.get(i1 + 1);
|
|
|
+ i1++;
|
|
|
+ System.out.println(sDate1+"-----"+eDate1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|