package com.monkeylessey.utils;
|
|
import com.monkeylessey.enums.WeekEnum;
|
import com.monkeylessey.exception.BaseException;
|
import org.springframework.util.StringUtils;
|
|
import javax.annotation.Nullable;
|
import java.sql.Timestamp;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.time.*;
|
import java.time.temporal.TemporalAdjusters;
|
import java.util.*;
|
|
/**
|
* 日期工具类
|
*
|
* @author xiangpei
|
* @date 2023/7/7
|
*/
|
public class DateUtil {
|
|
private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
|
|
/**
|
* 获取某天的开始时间
|
*
|
* @param date
|
* @return 2023-01-01 00:00:00
|
*/
|
public static Date getDayStart(@Nullable Date date) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("GMT+8"));
|
LocalDateTime of = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 0, 0, 0);
|
return Timestamp.valueOf(of);
|
}
|
|
/**
|
* 获取某天的结束时间
|
*
|
* @param date
|
* @return 2023-01-01 23:59:59
|
*/
|
public static Date getDayEnd(@Nullable Date date) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
LocalDateTime of = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 23, 59, 59);
|
return Timestamp.valueOf(of);
|
}
|
|
/**
|
* 获取某月的第一天
|
*
|
* @param date 2023-01-05
|
* @return 2023-01-01
|
*/
|
public static Date getMonthStart(@Nullable Date date) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
LocalDateTime start = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
|
return Timestamp.valueOf(start);
|
}
|
|
/**
|
* 获取某月的最后一天
|
*
|
* @param date
|
* @return
|
*/
|
public static Date getMonthEnd(@Nullable Date date) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
LocalDateTime end = localDateTime.with(TemporalAdjusters.lastDayOfMonth());
|
return Timestamp.valueOf(end);
|
}
|
|
/**
|
* 获取某年的最后一天
|
*
|
* @param date
|
* @return
|
*/
|
public static Date getYearEnd(@Nullable Date date) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
LocalDateTime end = localDateTime.with(TemporalAdjusters.lastDayOfYear());
|
return Timestamp.valueOf(end);
|
}
|
|
/**
|
* 获取某年的第一天
|
*
|
* @param date
|
* @return
|
*/
|
public static Date getYearStart(@Nullable Date date) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
LocalDateTime end = localDateTime.with(TemporalAdjusters.firstDayOfYear());
|
return Timestamp.valueOf(end);
|
}
|
|
/**
|
* 获取某天是星期几
|
*
|
* @param date
|
*/
|
public static WeekEnum getWeek(@Nullable Date date) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
|
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
|
for (WeekEnum weekDay : WeekEnum.values()) {
|
if (weekDay.getCode() == dayOfWeek) {
|
return weekDay;
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 格式化日期
|
*
|
* @param date
|
* @param pattern
|
* @return
|
*/
|
public static String formatDate(@Nullable Date date, @Nullable String pattern) {
|
if (Objects.isNull(date)) {
|
date = new Date();
|
}
|
if (!StringUtils.hasText(pattern)) {
|
pattern = DEFAULT_DATE_FORMAT;
|
}
|
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
return format.format(date);
|
}
|
|
/**
|
* 获取时间段内的每一天 每一月 每一年
|
*
|
* @param startDate
|
* @param endDate
|
* @param dateType
|
* @return
|
*/
|
public static List<String> getEveryItem(Date startDate, Date endDate, DateType dateType) throws ParseException {
|
if (Objects.isNull(startDate) || Objects.isNull(endDate)) {
|
throw new BaseException("传入时间不能为空");
|
}
|
checkTimeSize(startDate, endDate);
|
int diffNumber = 1;
|
List<String> result = new ArrayList<>(10);
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
SimpleDateFormat format;
|
switch (dateType) {
|
case DAY:
|
format = new SimpleDateFormat("yyyy-MM-dd");
|
diffNumber = (int) Math.abs((format.parse(format.format(startDate)).getTime() - format.parse(format.format(endDate)).getTime()) / (1000 * 3600 * 24)) + 1;
|
getEveryResult(diffNumber, format, calendar, Calendar.DAY_OF_MONTH, result);
|
break;
|
case MONTH:
|
format = new SimpleDateFormat("yyyy-MM");
|
Period between = Period.between(dateToLocal(format.parse(format.format(startDate))), dateToLocal(format.parse(format.format(endDate))));
|
diffNumber = between.getMonths() + between.getYears() * 12 + 1;
|
getEveryResult(diffNumber, format, calendar, Calendar.MONTH, result);
|
break;
|
case YEAR:
|
format = new SimpleDateFormat("yyyy");
|
diffNumber = Period.between(dateToLocal(format.parse(format.format(startDate))), dateToLocal(format.parse(format.format(endDate)))).getYears() + 1;
|
getEveryResult(diffNumber, format, calendar, Calendar.YEAR, result);
|
default:
|
break;
|
}
|
return result;
|
}
|
|
/**
|
* 获取时间范围内的天、月、年
|
*
|
* @param diffNumber
|
* @param format
|
* @param calendar
|
* @param calendarDateType
|
* @param result
|
*/
|
private static void getEveryResult(int diffNumber, SimpleDateFormat format, Calendar calendar, int calendarDateType, List<String> result) {
|
for (int i = 0; i < diffNumber; i++) {
|
result.add(format.format(calendar.getTime()));
|
calendar.add(calendarDateType, 1);
|
}
|
}
|
|
|
/**
|
* 检验开始时间不能大于结束时间
|
*
|
* @param startDate
|
* @param endDate
|
*/
|
public static void checkTimeSize(Date startDate, Date endDate) {
|
if (startDate.after(endDate)) {
|
throw new BaseException("开始时间不能大于结束时间");
|
}
|
}
|
|
/**
|
* date 转 localDate
|
*
|
* @param date
|
* @return
|
*/
|
public static LocalDate dateToLocal(Date date) {
|
if (Objects.isNull(date)) {
|
throw new BaseException("传入时间不能为空");
|
}
|
Instant instant = date.toInstant();
|
return instant.atZone(ZoneId.systemDefault()).toLocalDate();
|
}
|
|
|
public enum DateType {
|
|
DAY("获取时间段的每一天"),
|
MONTH("获取时间段的每一个月"),
|
YEAR("获取时间段的每一年");
|
|
private final String desc;
|
|
DateType(String desc) {
|
this.desc = desc;
|
}
|
}
|
}
|