2026-01-16 15:37:57 +08:00

242 lines
7.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.cdzy.user.utils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
/**
* 时间工具类
*
* @author yanglei
* @since 2026-01-16 14:29
*/
public class DateUtils {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 获取今天的开始时间00:00:00
*/
public static LocalDateTime getTodayStart() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
}
/**
* 获取今天的结束时间23:59:59
*/
public static LocalDateTime getTodayEnd() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
* 获取今天的时间范围
* @return 包含开始时间和结束时间的数组 [startTime, endTime]
*/
public static LocalDateTime[] getTodayRange() {
return new LocalDateTime[]{getTodayStart(), getTodayEnd()};
}
/**
* 获取近7天的时间范围包括今天
* @return 包含开始时间和结束时间的数组 [startTime, endTime]
*/
public static LocalDateTime[] getLast7DaysRange() {
LocalDateTime end = getTodayEnd();
LocalDateTime start = getTodayStart().minusDays(6);
return new LocalDateTime[]{start, end};
}
/**
* 获取近30天的时间范围包括今天
* @return 包含开始时间和结束时间的数组 [startTime, endTime]
*/
public static LocalDateTime[] getLast30DaysRange() {
LocalDateTime end = getTodayEnd();
LocalDateTime start = getTodayStart().minusDays(29);
return new LocalDateTime[]{start, end};
}
/**
* 获取指定天数前的时间范围
* @param days 天数
* @return 包含开始时间和结束时间的数组 [startTime, endTime]
*/
public static LocalDateTime[] getLastDaysRange(int days) {
LocalDateTime end = getTodayEnd();
LocalDateTime start = getTodayStart().minusDays(days - 1);
return new LocalDateTime[]{start, end};
}
/**
* 获取本周的时间范围
* @return 包含开始时间和结束时间的数组 [startTime, endTime]
*/
public static LocalDateTime[] getThisWeekRange() {
LocalDate today = LocalDate.now();
LocalDate startOfWeek = today.with(java.time.DayOfWeek.MONDAY);
LocalDate endOfWeek = today.with(java.time.DayOfWeek.SUNDAY);
LocalDateTime start = LocalDateTime.of(startOfWeek, LocalTime.MIN);
LocalDateTime end = LocalDateTime.of(endOfWeek, LocalTime.MAX);
return new LocalDateTime[]{start, end};
}
/**
* 获取本月的时间范围
* @return 包含开始时间和结束时间的数组 [startTime, endTime]
*/
public static LocalDateTime[] getThisMonthRange() {
LocalDate today = LocalDate.now();
LocalDate startOfMonth = today.withDayOfMonth(1);
LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth());
LocalDateTime start = LocalDateTime.of(startOfMonth, LocalTime.MIN);
LocalDateTime end = LocalDateTime.of(endOfMonth, LocalTime.MAX);
return new LocalDateTime[]{start, end};
}
/**
* 获取今天日期字符串yyyy-MM-dd
*/
public static String getTodayDateString() {
return LocalDate.now().format(DATE_FORMATTER);
}
/**
* 获取现在日期时间字符串yyyy-MM-dd HH:mm:ss
*/
public static String getNowDateTimeString() {
return LocalDateTime.now().format(DATETIME_FORMATTER);
}
/**
* 获取近7天每天的日期列表包括今天
* @return 日期字符串列表格式yyyy-MM-dd
*/
public static List<String> getLast7DaysDateList() {
return getDateList(7);
}
/**
* 获取近30天每天的日期列表包括今天
* @return 日期字符串列表格式yyyy-MM-dd
*/
public static List<String> getLast30DaysDateList() {
return getDateList(30);
}
/**
* 获取指定天数的日期列表
* @param days 天数
* @return 日期字符串列表格式yyyy-MM-dd
*/
public static List<String> getDateList(int days) {
List<String> dateList = new ArrayList<>();
LocalDate today = LocalDate.now();
for (int i = days - 1; i >= 0; i--) {
LocalDate date = today.minusDays(i);
dateList.add(date.format(DATE_FORMATTER));
}
return dateList;
}
/**
* 获取两个日期之间的所有日期列表
* @param startDate 开始日期
* @param endDate 结束日期
* @return 日期字符串列表格式yyyy-MM-dd
*/
public static List<String> getDateRangeList(LocalDate startDate, LocalDate endDate) {
List<String> dateList = new ArrayList<>();
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
for (long i = 0; i <= daysBetween; i++) {
LocalDate date = startDate.plusDays(i);
dateList.add(date.format(DATE_FORMATTER));
}
return dateList;
}
/**
* 格式化LocalDateTime为字符串
* @param dateTime 时间
* @return 格式化后的字符串 yyyy-MM-dd HH:mm:ss
*/
public static String formatDateTime(LocalDateTime dateTime) {
return dateTime != null ? dateTime.format(DATETIME_FORMATTER) : null;
}
/**
* 格式化LocalDate为字符串
* @param date 日期
* @return 格式化后的字符串 yyyy-MM-dd
*/
public static String formatDate(LocalDate date) {
return date != null ? date.format(DATE_FORMATTER) : null;
}
/**
* 字符串转LocalDateTime
* @param dateTimeStr 时间字符串 yyyy-MM-dd HH:mm:ss
*/
public static LocalDateTime parseDateTime(String dateTimeStr) {
return LocalDateTime.parse(dateTimeStr, DATETIME_FORMATTER);
}
/**
* 字符串转LocalDate
* @param dateStr 日期字符串 yyyy-MM-dd
*/
public static LocalDate parseDate(String dateStr) {
return LocalDate.parse(dateStr, DATE_FORMATTER);
}
/**
* 获取指定日期的开始时间
* @param date 日期
* @return 当天的开始时间
*/
public static LocalDateTime getStartOfDay(LocalDate date) {
return LocalDateTime.of(date, LocalTime.MIN);
}
/**
* 获取指定日期的结束时间
* @param date 日期
* @return 当天的结束时间
*/
public static LocalDateTime getEndOfDay(LocalDate date) {
return LocalDateTime.of(date, LocalTime.MAX);
}
/**
* 获取指定日期的前n天
* @param date 基准日期
* @param days 天数
* @return 前n天的日期
*/
public static LocalDate getDaysBefore(LocalDate date, int days) {
return date.minusDays(days);
}
/**
* 获取指定日期的后n天
* @param date 基准日期
* @param days 天数
* @return 后n天的日期
*/
public static LocalDate getDaysAfter(LocalDate date, int days) {
return date.plusDays(days);
}
}