【按月、季度、半年、年将时间区间切分】修复splitToHalfOfYears按半年切割
//将时间段按星期分割
public static final List splitToWeeks(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Date from = new Date(start.getTime() + 7L * 24 * 3600 * 1000);
Date weekEnd = cn.hutool.core.date.DateUtil.endOfWeek(end);
while (from.compareTo(weekEnd) <= 0) {
Date dt = cn.hutool.core.date.DateUtil.beginOfWeek(from);
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
result.add(Range.create(dt));
from.setTime(from.getTime() + 7L * 24 * 3600 * 1000);
}
CollectionUtil.getLast(result).end(end);
return result;
}//将时间段按照月分割
public static final List splitToMonths(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MONTH, 1);
Date monthEnd = cn.hutool.core.date.DateUtil.endOfMonth(end);
while (cal.getTimeInMillis() <= monthEnd.getTime()) {
Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
result.add(Range.create(dt));
cal.add(Calendar.MONTH, 1);
}
CollectionUtil.getLast(result).end(end);
return result;
}//将时间段按照季度分割
public static final List splitToQuarts(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MONTH, 3);
Date quartEnd = cn.hutool.core.date.DateUtil.endOfQuarter(end);
while (cal.getTimeInMillis() <= quartEnd.getTime()) {
Date dt = cn.hutool.core.date.DateUtil.beginOfQuarter(cal.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
result.add(Range.create(dt));
cal.add(Calendar.MONTH, 3);
}
CollectionUtil.getLast(result).end(end);
return result;
}//将时间段按照半年分割
public static final List splitToHalfOfYears(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MONTH, 6);
int mon = cal.get(Calendar.MONTH);
int tmpMon = DateUtil.month(end);
Calendar tmp = Calendar.getInstance();
if (tmpMon < 6) {
tmp.setTimeInMillis(cal.getTimeInMillis());
tmp.set(Calendar.MONTH, 5);
} else {
tmp.setTimeInMillis(cal.getTimeInMillis());
tmp.set(Calendar.MONTH, 11);
}
tmp.set(Calendar.YEAR, DateUtil.year(end));
Calendar halfYearEnd = cn.hutool.core.date.DateUtil.endOfMonth(tmp);
long timeInMillis = halfYearEnd.getTimeInMillis();
while (cal.getTimeInMillis() <= timeInMillis) {
mon = cal.get(Calendar.MONTH);
Calendar cal1 = Calendar.getInstance();
if (mon < 6) {
cal1.setTimeInMillis(cal.getTimeInMillis());
cal1.set(Calendar.MONTH, 0);
} else {
cal1.setTimeInMillis(cal.getTimeInMillis());
cal1.set(Calendar.MONTH, 6);
}
Date dt = cn.hutool.core.date.DateUtil.beginOfMonth(cal1.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
result.add(Range.create(dt));
cal.add(Calendar.MONTH, 6);
}
CollectionUtil.getLast(result).end(end);
return result;
}//将时间段按照年分割
public static final List splitToYears(Date start, Date end) {
List result = new ArrayList<>();
result.add(Range.create(start));
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.YEAR, 1);
Date yearEnd = cn.hutool.core.date.DateUtil.endOfYear(end);
while (cal.getTimeInMillis() <= yearEnd.getTime()) {
Date dt = cn.hutool.core.date.DateUtil.beginOfYear(cal.getTime());
CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
result.add(Range.create(dt));
cal.add(Calendar.YEAR, 1);
}
CollectionUtil.getLast(result).end(end);
return result;
}//日期区间
public static class Range {
Date start;
Date end;
private Range(Date start) {
this.start = start;
}public static Range create(Date start) {
return new Range(start);
}public Range end(Date end) {
this.end = end;
return this;
}public Date getStart() {
return start;
}public Date getEnd() {
return end;
}@Override
public String toString() {
return "[" + cn.hutool.core.date.DateUtil.format(start, "yyyy-MM-dd") + "-" + cn.hutool.core.date.DateUtil.format(end, "yyyy-MM-dd")
+ "]";
}
}