获取 几小时内、几天内、几周内、几月内

/**
* 获取 几小时内、几天内、几周内、几月内
* @param dateType
* @param dateValue
* @return
*/
public static LocalDateTime getDateType(Integer dateType,int dateValue){
if(DateTypeEnum.HOURS.getCode().equals(dateType)){
return differentHours(dateValue,LocalDateTime.now());
}else if(DateTypeEnum.DAY.getCode().equals(dateType)){
return differentDays(dateValue,LocalDateTime.now());
}else if(DateTypeEnum.WEK.getCode().equals(dateType)){
return differentWeek(dateValue,LocalDate.now());
}else if(DateTypeEnum.MONTH.getCode().equals(dateType)){
return differentMonth(dateValue,LocalDate.now());
}
return null;
}

/**
* 当前时间几小时内
* @param dateValue
* @return
*/
public static LocalDateTime differentHours(Integer dateValue, LocalDateTime currentTime) {
dateValue = dateValue - 1;
return currentTime.plusHours(-dateValue);
}

/**
* 当前时间几天内
* @param dateValue
* @return
*/
public static LocalDateTime differentDays(Integer dateValue,LocalDateTime currentTime) {
dateValue = dateValue - 1;
return currentTime.plusDays(-dateValue);
}

/**
* 获取几周内的时间
* @param dateValue
* @return
*/
public static LocalDateTime differentWeek(Integer dateValue,LocalDate currentTime ){
dateValue = dateValue - 1;
return getStartDayOfWeek(currentTime).plusWeeks( - dateValue);
}

/**
* 获取几月内的时间
* @param dateValue
* @return
*/
public static LocalDateTime differentMonth(Integer dateValue,LocalDate currentTime ){
dateValue = dateValue - 1;
return getStartDayOfMonth(currentTime).plusMonths(-dateValue);
}


/**
* 本周开始 支持跨年
* @param today
* @return
*/
public static LocalDateTime getStartDayOfWeek(LocalDate today) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
DayOfWeek week = today.getDayOfWeek();
int value = week.getValue();
resDate = today.minusDays(value - 1);
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + minDate);
return localDateTime;
}


/**
* 本月的开始
* @param today
* @return
*/
public static LocalDateTime getStartDayOfMonth(LocalDate today) {
LocalDate resDate = LocalDate.now();
if (today == null) {
today = resDate;
}
Month month = today.getMonth();
resDate = LocalDate.of(today.getYear(), month, 1);
LocalDateTime localDateTime = LocalDateTime.parse(resDate.toString() + minDate);
return localDateTime;
}
posted @ 2022-12-29 11:23  爵士灬  阅读(60)  评论(0编辑  收藏  举报