关于日期连续显示问题
public static void main(String[] args) {
LocalDateTime startTime = LocalDateTime.now();
List<LocalDate> dates = new ArrayList<>(45);
dates.add(LocalDate.now().minusYears(1).minusDays(6));
dates.add(LocalDate.now().minusYears(1).minusDays(5));
dates.add(LocalDate.now().minusYears(1).minusDays(2));
dates.add(LocalDate.now().minusYears(1).minusDays(1));
dates.add(LocalDate.now().minusYears(1));
LocalDate localDate = LocalDate.now().minusMonths(3);
for (int i = 0; i < 20; i++) {
dates.add(localDate.plusDays(i));
}
localDate = LocalDate.now().minusMonths(1);
for (int i = 0; i < 20; i++) {
dates.add(localDate.plusDays(i));
}
System.out.println("============处理之前============");
for (LocalDate date : dates) {
System.out.println(date);
}
List<String> list = getAcronymDateForContinuity(dates);
System.out.println("============处理之后============");
for (String str : list) {
System.out.println(str);
}
System.out.println("总耗时:" + (LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() - startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()) + "ms");
System.out.println("============还原============");
List<LocalDate> localDates = getContinuousDate(list);
for (LocalDate date : localDates) {
System.out.println(date);
}
}
private static List<LocalDate> getContinuousDate(List<String> dates) {
List<LocalDate> localDates = new LinkedList<>();
LocalDate beginDate;
LocalDate endDate;
for (String str : dates) {
String[] strArr = str.split("\\~");
if (strArr.length > 1) {
beginDate = LocalDate.parse(strArr[0].trim(), DateTimeFormatter.ISO_DATE);
localDates.add(beginDate);
endDate = LocalDate.parse(strArr[strArr.length - 1].trim(), DateTimeFormatter.ISO_DATE);
while (true) {
beginDate = beginDate.plusDays(1);
if (!beginDate.equals(endDate)) {
localDates.add(beginDate);
} else {
break;
}
}
localDates.add(endDate);
} else {
beginDate = LocalDate.parse(strArr[0].trim(), DateTimeFormatter.ISO_DATE);
localDates.add(beginDate);
}
}
return localDates;
}
private static List<String> getAcronymDateForContinuity(List<LocalDate> dates) {
List<String> dateStrList = new ArrayList<>(dates.size());
int result;
for (int i = 0; i < dates.size(); i++) {
result = getLastIndexForContinuousDate(i, dates);
if (result == i) {
dateStrList.add(dates.get(i).toString());
} else if (result == i + 1) {
dateStrList.add(dates.get(i).toString());
dateStrList.add(dates.get(result).toString());
i = result;
} else if (result > i + 1) {
dateStrList.add(dates.get(i) + " ~ " + dates.get(result));
i = result;
}
}
return dateStrList;
}
private static int getLastIndexForContinuousDate(int begin, List<LocalDate> dates) {
if (begin < dates.size() - 1) {
if (dates.get(begin).plusDays(1).equals(dates.get(++begin))) {
if (begin < dates.size() - 1) {
return getLastIndexForContinuousDate(begin, dates);
}
return begin;
}
}
return begin - 1;
}
测试输出
============处理之前============
2020-10-15
2020-10-16
2020-10-19
2020-10-20
2020-10-21
2021-07-21
2021-07-22
2021-07-23
2021-07-24
2021-07-25
2021-07-26
2021-07-27
2021-07-28
2021-07-29
2021-07-30
2021-07-31
2021-08-01
2021-08-02
2021-08-03
2021-08-04
2021-08-05
2021-08-06
2021-08-07
2021-08-08
2021-08-09
2021-09-21
2021-09-22
2021-09-23
2021-09-24
2021-09-25
2021-09-26
2021-09-27
2021-09-28
2021-09-29
2021-09-30
2021-10-01
2021-10-02
2021-10-03
2021-10-04
2021-10-05
2021-10-06
2021-10-07
2021-10-08
2021-10-09
2021-10-10
============处理之后============
2020-10-15
2020-10-16
2020-10-19 ~ 2020-10-21
2021-07-21 ~ 2021-08-09
2021-09-21 ~ 2021-10-10
总耗时:2ms
============还原============
2020-10-15
2020-10-16
2020-10-19
2020-10-20
2020-10-21
2021-07-21
2021-07-22
2021-07-23
2021-07-24
2021-07-25
2021-07-26
2021-07-27
2021-07-28
2021-07-29
2021-07-30
2021-07-31
2021-08-01
2021-08-02
2021-08-03
2021-08-04
2021-08-05
2021-08-06
2021-08-07
2021-08-08
2021-08-09
2021-09-21
2021-09-22
2021-09-23
2021-09-24
2021-09-25
2021-09-26
2021-09-27
2021-09-28
2021-09-29
2021-09-30
2021-10-01
2021-10-02
2021-10-03
2021-10-04
2021-10-05
2021-10-06
2021-10-07
2021-10-08
2021-10-09
2021-10-10