由于使用到了hutool的部分工具类
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.7</version>
</dependency>
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;
public class TimePeriodUtil {
public static List<String> getTimeDivisionByDateType(DateTypeEnum dateType,String startTime,String endTime){
switch (dateType){
case QUARTER:
return convertWeekList(startTime,endTime);
case YEAR:
return getMonthPeriods(startTime, endTime);
default:
return getBetweenDate(startTime,endTime);
}
}
/**
* 得到当前时间与当前季度的间隔天数
* @param time 当前时间
* @return
*/
public static Long getQuarterTimeInterval(Date time){
String endTime = DateUtil.format(time, "yyyy-MM-dd");
return getQuarterTimeInterval(endTime);
}
public static Long getQuarterTimeInterval(String time){
String startTime = getStartOrEndDayOfQuarter(LocalDateTimeUtil.parseDate(time), true);
long betweenDay = DateUtil.between(DateUtil.parse(startTime,"yyyy-MM-dd"), DateUtil.parse(time,"yyyy-MM-dd"), DateUnit.DAY);
return betweenDay+1;
}
public static List<String> getMonthPeriod(){
List<String> list = new ArrayList<>();
DateTime begin = DateUtil.beginOfYear(DateUtil.yesterday());
DateTime end = DateUtil.beginOfMonth(DateUtil.yesterday());
while (DateUtil.between(begin, end, DateUnit.DAY) != 0){
list.add(DateUtil.format(begin, "yyyy-MM"));
begin = DateUtil.offsetMonth(begin,1);
}
list.add(DateUtil.format(end, "yyyy-MM"));
return list;
}
public static List<String> getMonthPeriods(String startTime,String endTime){
List<String> list = new ArrayList<>();
DateTime begin = DateUtil.parse(startTime, "yyyy-MM");
DateTime end = DateUtil.parse(endTime, "yyyy-MM");
while (DateUtil.between(begin, end, DateUnit.DAY) != 0){
list.add(DateUtil.format(begin,"yyyy-MM"));
begin = DateUtil.offsetMonth(begin,1);
}
list.add(DateUtil.format(end,"yyyy-MM"));
return list;
}
/**
* 得到时间段类,按照周分段
* 比如传入("2021-01-01","2020-01-06")
* 返回[2021-01-01~2021-01-03, 2021-01-04~2021-01-06]
* @param beginTime 2021-01-01
* @param endTime 2020-01-06
* @return [2021-01-01~2021-01-03, 2021-01-04~2021-01-06]
*/
public static List<String> convertWeekList(String beginTime, String endTime){
LocalDate startDate =LocalDate.parse(beginTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate endDate =LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
List<String> weekList = new ArrayList<>();
LocalDate firstDay = startDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate lastDay = endDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
long days = ChronoUnit.DAYS.between(firstDay, lastDay);
if (days > 0){
int weekLength = 7;
for(int i=0;i<days;i=i+weekLength){
String monDay = firstDay.plusDays(i).toString();
String sunDay = firstDay.plusDays(i+6).toString();
String week = monDay+"~"+sunDay;
weekList.add(week);
}
}
String start = startDate.toString();
String end = weekList.get(0).split("~")[1];
weekList.set(0,start+"~"+end);
String lastStart = weekList.get(weekList.size()-1).split("~")[0];
String lastEnd = endDate.toString();
weekList.set(weekList.size()-1,lastStart+"~"+lastEnd);
return weekList;
}
/**
* 获取当前时间所在季度的第一天
*/
public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst){
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirst) {
resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
} else {
resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
}
return resDate.toString();
}
/**
* 获取一段时间的每一天日期
*/
public static List<String> getBetweenDate(String start, String end) {
List<String> list = new ArrayList<>();
LocalDate startDate = LocalDate.parse(start);
LocalDate endDate = LocalDate.parse(end);
long distance = ChronoUnit.DAYS.between(startDate, endDate);
if (distance < 1) {
list.add(start);
return list;
}
Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString()));
return list;
}
public static void main(String[] args) {
List<String> list = convertWeekList("2021-01-01", "2021-01-19");
System.out.println(list.toString());
Long quarterTimeInterval = getQuarterTimeInterval("2021-03-01");
System.out.println(quarterTimeInterval);
}
}