Files
metis/metis-starter/src/main/java/com/metis/utils/LocalDateTimeUtils.java

400 lines
11 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.metis.utils;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalUnit;
import java.util.Date;
/**
* 地方日期时间工具类
*
* @author ZhangQiang
* @date 2024/10/28
*/
public class LocalDateTimeUtils extends cn.hutool.core.date.LocalDateTimeUtil {
/**
* 在范围内
*
* @param target 目标
* @param start 开始
* @param end 结束
* @return boolean
*/
public static boolean isWithinRange(LocalDateTime target, LocalDateTime start, LocalDateTime end) {
return !target.isBefore(start) && !target.isAfter(end);
}
/**
* 现在时间在范围内
*
* @param start 开始
* @param end 结束
* @return boolean
*/
public static boolean nowIsWithinRange(LocalDateTime start, LocalDateTime end) {
LocalDateTime target = LocalDateTime.now();
return !target.isBefore(start) && !target.isAfter(end);
}
/**
* 获取指定时间是周几
*
* @param time 时间
* @return int
*/
public static int week(LocalDateTime time) {
return time.getDayOfWeek().getValue();
}
/**
* 获取加或减N月的第一天
*
* @param num 数字
* @return {@link LocalDateTime}
*/
public static LocalDateTime monthFirst(int num) {
LocalDateTime newTime = plus(LocalDateTime.now(), num, ChronoUnit.MONTHS);
newTime = newTime.with(TemporalAdjusters.firstDayOfMonth());
return getDayStart(newTime);
}
/**
* 获取加或减N月的最后天
*
* @param num 数字
* @return {@link LocalDateTime}
*/
public static LocalDateTime monthLast(int num) {
LocalDateTime newTime = plus(LocalDateTime.now(), num, ChronoUnit.MONTHS);
newTime = newTime.with(TemporalAdjusters.lastDayOfMonth());
return getDayEnd(newTime);
}
/**
* 获取加或减N周的第一天
*
* @param num 数字
* @return {@link LocalDateTime}
*/
public static LocalDateTime weekFirst(int num) {
int week = week(LocalDateTime.now());
LocalDateTime newTime = subtract(LocalDateTime.now(), week - 1, ChronoUnit.DAYS);
newTime = plus(newTime, num * 7L, ChronoUnit.DAYS);
return getDayStart(newTime);
}
/**
* 获取加或减N周的最后一天
*
* @param num 数字
* @return {@link LocalDateTime}
*/
public static LocalDateTime weekLast(int num) {
int week = week(LocalDateTime.now());
LocalDateTime newTime = plus(LocalDateTime.now(), 7 - week, ChronoUnit.DAYS);
newTime = plus(newTime, num * 7L, ChronoUnit.DAYS);
return getDayEnd(newTime);
}
/**
* 判断时间 ==> t1 < t2 = true 2019-10-13 11:11:00 < 2020-11-13 13:13:00 = true
*
* @param t1 t1
* @param t2 t2
* @return boolean
*/
public static boolean isBefore(LocalDateTime t1, LocalDateTime t2) {
return t1.isBefore(t2);
}
/**
* 判断时间 ==> t1 > t2 = true2019-10-13 11:11:00 > 2020-11-13 13:13:00 = false
*
* @param t1 t1
* @param t2 t2
* @return boolean
*/
public static boolean isAfter(LocalDateTime t1, LocalDateTime t2) {
return t1.isAfter(t2);
}
/**
* Date 转 LocalDateTime
*
* @param date 日期
* @return {@link LocalDateTime}
*/
public static LocalDateTime convertToLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
/**
* LocalDateTime 转 Date
*
* @param time 时间
* @return {@link Date}
*/
public static Date convertToDate(LocalDateTime time) {
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* Date转LocalDate
*
* @param date 日期
* @return {@link LocalDate}
*/
public static LocalDate convertLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* Date转LocalDate
*
* @param date 日期
* @return {@link LocalTime}
*/
public static LocalTime convertLocalTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
}
/**
* 获取指定日期的毫秒
*
* @param time 时间
* @return {@link Long}
*/
public static Long getMilliByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 获取秒数通过时间
* 获取指定日期的秒
*
* @param time 时间
* @return {@link Long}
*/
public static Long getSecondsByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
}
/**
* 获取指定时间的指定格式 ==> yyyy-MM-dd HH:mm:ss:SSS (HH是24小时制而hh是12小时制, ss是秒SSS是毫秒)
*
* @param time 时间
* @param pattern 图案
* @return {@link String}
*/
public static String formatTime(LocalDateTime time, String pattern) {
return time.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* 日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
*
* @param time 时间
* @param number 数
* @param field 领域
* @return {@link LocalDateTime}
*/
public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
return time.plus(number, field);
}
/**
* 减去
* 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
*
* @param time 时间
* @param number 数
* @param field 领域
* @return {@link LocalDateTime}
*/
public static LocalDateTime subtract(LocalDateTime time, long number, TemporalUnit field) {
return time.minus(number, field);
}
/**
* 获取白天开始
* 获取指定某一天的开始时间 00:00:00
*
* @param time 时间
* @return {@link LocalDateTime}
*/
public static LocalDateTime getDayStart(LocalDateTime time) {
return time.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
}
/**
* 获取白天结束
* 获取指定某一天的结束时间 23:59:59.999
*
* @param time 时间
* @return {@link LocalDateTime}
*/
public static LocalDateTime getDayEnd(LocalDateTime time) {
return time.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(999999999);
}
/**
* 获取本周周一
*
* @param time 时间
* @return {@link LocalDateTime}
*/
public static LocalDateTime getWeekOfFirst(LocalDateTime time) {
return time.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).
plusDays(1).withHour(0).withMinute(0).withSecond(0);
}
/**
* 获取本周周日
*
* @param time 时间
* @return {@link LocalDateTime}
*/
public static LocalDateTime getWeekOfLast(LocalDateTime time) {
return time.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).
minusDays(1).withHour(23).withMinute(59).withSecond(59);
}
/**
* 获取本月第一天
*
* @param time 时间
* @return {@link LocalDateTime}
*/
public static LocalDateTime getMonthOfFirst(LocalDateTime time) {
LocalDateTime firsthand = time.with(TemporalAdjusters.firstDayOfMonth());
return LocalDateTime.of(firsthand.toLocalDate(), LocalTime.MIN);
}
/**
* 获取本月最后一天
*
* @param time 时间
* @return {@link LocalDateTime}
*/
public static LocalDateTime getMonthOfLast(LocalDateTime time) {
LocalDateTime lastDay = time.with(TemporalAdjusters.lastDayOfMonth());
return LocalDateTime.of(lastDay.toLocalDate(), LocalTime.MAX);
}
/**
* 日期相隔天数
*
* @param startDateInclusive 开始日期(含)
* @param endDateExclusive 结束日期除外
* @return int
*/
public static int periodDays(LocalDate startDateInclusive, LocalDate endDateExclusive) {
return Period.between(startDateInclusive, endDateExclusive).getDays();
}
/**
* 日期相隔小时
*
* @param startInclusive 开始包容性
* @param endExclusive 结束独家
* @return long
*/
public static long durationHours(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toHours();
}
/**
* 日期相隔分钟
*
* @param startInclusive 开始包容性
* @param endExclusive 结束独家
* @return long
*/
public static long durationMinutes(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toMinutes();
}
/**
* 持续时间秒
*
* @param startInclusive 开始包容
* @param endExclusive 结束独家
* @return long
*/
public static long durationSeconds(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toSeconds();
}
/**
* 日期相隔毫秒数
*
* @param startInclusive 开始包容性
* @param endExclusive 结束独家
* @return long
*/
public static long durationMillis(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toMillis();
}
/**
* 是否当天
*/
public static boolean isToday(LocalDate date) {
return LocalDate.now().equals(date);
}
/**
* 判断两个时间是否相差指定时间(精确到毫秒)
*
* @param time1 第一个时间点
* @param time2 第二个时间点
* @param milliseconds 指定时间差(毫秒)
* @return true 如果两个时间相差超过指定毫秒数,否则 false
*/
public static boolean isTimeDifferenceExceeds(LocalDateTime time1, LocalDateTime time2, long milliseconds) {
if (time1 == null || time2 == null) {
throw new IllegalArgumentException("Time arguments cannot be null");
}
Duration duration = Duration.between(time1, time2);
return Math.abs(duration.toMillis()) > milliseconds;
}
/**
* 判断指定时间是否与当前时间相差超过 24 小时(精确到毫秒)
*
* @param targetTime 指定的 LocalDateTime 时间
* @return true 如果相差 24 小时以上,否则 false
*/
public static boolean isMoreThan24HoursApart(LocalDateTime targetTime) {
if (targetTime == null) {
throw new IllegalArgumentException("Target time cannot be null");
}
// 比较时间差的绝对值是否大于 24 小时
return isTimeDifferenceExceeds(LocalDateTime.now(), targetTime, 86400000L);
}
}