package com.test.util;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;
/**
* @className: LocalDateTimeUtil
* @description: LocalDateTime 工具类
* @author: tanjunjun
* @date: 2022/7/1
**/
public class LocalDateTimeUtil {
/**
* 计算两个时间相差多少个小时 结束时间 - 开始时间
* @param start 开始时间
* @param end 结束时间
* @return 整数
*/
public static Long calculateTimeDiff(LocalDateTime start, LocalDateTime end){
if (Objects.isNull(start) || Objects.isNull(end)) {
return 0L;
}
Duration duration = Duration.between(start,end);
return duration.toHours();
}
/**
* date转localDate
*/
public static LocalDateTime transformLocalDate(Date date){
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
//Date转换为LocalDateTime
return LocalDateTime.ofInstant(instant, zone);
}
/**
* 获取几天后的当前时间
* @return
*/
public static LocalDateTime getOverDays(Long days,LocalDateTime time){
return time.plusDays(days);
}
/**
* 获取指定日期的的几月几号几点几分几秒
* @param time
* @return
*/
public static LocalDateTime planCompleteTime(LocalDateTime time){
// 指定月份的4号12点
return time.withMonth(7).withDayOfMonth(4).withHour(12).withMinute(0).withSecond(0).withNano(0);
}
}
版权声明:本文为weixin_47252341原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。