add methods

This commit is contained in:
Looly 2024-05-23 17:33:28 +08:00
parent 3daeac4631
commit abb87d530b
2 changed files with 75 additions and 4 deletions

View File

@ -61,6 +61,7 @@ public class TemporalUtil {
* @param unit 被转换的{@link TimeUnit}单位如果为{@code null}返回{@code null}
* @return {@link ChronoUnit}
* @since 5.7.16
* @throws IllegalArgumentException Unknown TimeUnit
*/
public static ChronoUnit toChronoUnit(final TimeUnit unit) throws IllegalArgumentException {
if (null == unit) {

View File

@ -444,6 +444,16 @@ public class TimeUtil extends TemporalAccessorUtil {
return time.with(LocalTime.MIN);
}
/**
* 修改为一天的开始时间例如2020-02-02 00:00:00,000
*
* @param date 日期
* @return 一天的开始时间
*/
public static LocalDateTime beginOfDay(final LocalDate date) {
return date.atStartOfDay();
}
/**
* 修改为一天的结束时间例如
* <ul>
@ -460,6 +470,22 @@ public class TimeUtil extends TemporalAccessorUtil {
return time.with(LocalTimeUtil.max(truncateMillisecond));
}
/**
* 修改为一天的结束时间例如
* <ul>
* <li>毫秒不归零2020-02-02 23:59:59,999</li>
* <li>毫秒归零2020-02-02 23:59:59,000</li>
* </ul>
*
* @param date 日期
* @param truncateMillisecond 是否毫秒归零
* @return 一天的结束时间
* @since 5.7.18
*/
public static LocalDateTime endOfDay(final LocalDate date, final boolean truncateMillisecond) {
return LocalDateTime.of(date, LocalTimeUtil.max(truncateMillisecond));
}
/**
* 修改为月初的开始时间例如2020-02-01 00:00:00,000
*
@ -468,7 +494,18 @@ public class TimeUtil extends TemporalAccessorUtil {
* @since 6.0.0
*/
public static LocalDateTime beginOfMonth(final LocalDateTime time) {
return beginOfDay(time).with(TemporalAdjusters.firstDayOfMonth());
return beginOfDay(beginOfMonth(time.toLocalDate()));
}
/**
* 修改为月初的开始时间例如2020-02-01 00:00:00,000
*
* @param date 日期
* @return 月初的开始时间
* @since 6.0.0
*/
public static LocalDate beginOfMonth(final LocalDate date) {
return date.with(TemporalAdjusters.firstDayOfMonth());
}
/**
@ -480,7 +517,18 @@ public class TimeUtil extends TemporalAccessorUtil {
* @since 6.0.0
*/
public static LocalDateTime endOfMonth(final LocalDateTime time, final boolean truncateMillisecond) {
return endOfDay(time, truncateMillisecond).with(TemporalAdjusters.lastDayOfMonth());
return endOfDay(endOfMonth(time.toLocalDate()), truncateMillisecond);
}
/**
* 修改为月底的结束时间
*
* @param date 日期
* @return 月底的结束时间
* @since 6.0.0
*/
public static LocalDate endOfMonth(final LocalDate date) {
return date.with(TemporalAdjusters.lastDayOfMonth());
}
/**
@ -491,7 +539,18 @@ public class TimeUtil extends TemporalAccessorUtil {
* @since 6.0.0
*/
public static LocalDateTime beginOfYear(final LocalDateTime time) {
return beginOfDay(time).with(TemporalAdjusters.firstDayOfYear());
return beginOfDay(beginOfYear(time.toLocalDate()));
}
/**
* 修改为一年的开始时间例如2020-01-01 00:00:00,000
*
* @param date 日期
* @return 一年的开始时间
* @since 6.0.0
*/
public static LocalDate beginOfYear(final LocalDate date) {
return date.with(TemporalAdjusters.firstDayOfYear());
}
/**
@ -503,7 +562,18 @@ public class TimeUtil extends TemporalAccessorUtil {
* @since 6.0.0
*/
public static LocalDateTime endOfYear(final LocalDateTime time, final boolean truncateMillisecond) {
return endOfDay(time, truncateMillisecond).with(TemporalAdjusters.lastDayOfYear());
return endOfDay(endOfYear(time.toLocalDate()), truncateMillisecond);
}
/**
* 修改为一年的结束时间
*
* @param date 日期
* @return 一年的结束时间
* @since 6.0.0
*/
public static LocalDate endOfYear(final LocalDate date) {
return date.with(TemporalAdjusters.lastDayOfYear());
}
/**