时间工具类

由于使用到了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);
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值