import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* @author fanghui
* @create 2020-4-8 18:38
**/
public class LocalDateUtil {
/**
* @param localDate
* @return
* @Author: fanghui
* @Date: 2020-04-08 05:21:59
* @Description: LocalDate转Date
**/
public static Date localDate2Date(LocalDate localDate) {
if (null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
/**
* @param date
* @return
* @Author: fanghui
* @Date: 2020-04-08 05:21:51
* @Description: Date转LocalDate
**/
public static LocalDate date2LocalDate(Date date) {
if (null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* @param date
* @return
* @Author: fanghui
* @Date: 2021-04-08 05:21:51
* @Description: 截止当前时间之前的时间段(之前/之后)
**/
public static boolean date2LocalDate(Date date) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
now.isAfter(LocalDateTime.parse(u.getFromTime(), pattern))
now.isBefore(LocalDateTime.parse(u.getToTime(), pattern)))
可以这样使用:
//fromTime<now<toTime置为推送中
List<UpgradePushListVO> pushExist = list.stream()
.filter(u -> now.isAfter(LocalDateTime.parse(u.getFromTime(), pattern)) &&
now.isBefore(LocalDateTime.parse(u.getToTime(), pattern)))
.collect(toList());
log.info("当前时间大于开始时间,小于结束时间,推送中,pushIds:【{}】",pushExist.stream().map(UpgradePushListVO::getId).collect(toList()));
或者使用下面的getTimeDifference方法返回的也是boolean 类型
Duration.between(LocalDateTime.now(), examinationResponse.getEndTime()).isNegative()
}
/**
* @param endTime
* @param startTime
* @return
* @Author: fanghui
* @Date: 2020/12/18
* @Description: 获取两个时间相差时分秒
**/
private String getTimeDifference(LocalDateTime endTime, LocalDateTime startTime) {
Duration duration = Duration.between(startTime, endTime);
long hour = duration.getSeconds() / ChronoUnit.HOURS.getDuration().getSeconds();
long minute = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) / ChronoUnit.MINUTES.getDuration().getSeconds();
long second = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) - minute * ChronoUnit.MINUTES.getDuration().getSeconds();
return hour + "时" + minute + "分" + second + "秒";
}
/**
* @param date
* @return
* @Author: fanghui
* @Date: 2020-04-08 05:22:09
* @Description: Date转换为LocalDateTime
**/
public static LocalDateTime date2LocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
return localDateTime;
}
/**
* @param localDateTime
* @return
* @Author: fanghui
* @Date: 2020-04-08 05:22:54
* @Description: LocalDateTime转换为Date
**/
public static Date localDateTime2Date(LocalDateTime localDateTime) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
Date date = Date.from(zdt.toInstant());
return date;
}
public static LocalDateTime strTranslocalDateTime(String str) {
if (MyUtil.isBlank(str)) {
return null;
}
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(str, df);
return ldt;
}
public static String localDateTimeTransStr(LocalDateTime localDateTime) {
if (MyUtil.isBlank(localDateTime)) {
return null;
}
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String localTime = df.format(localDateTime);
return localTime;
}
}
java8日期类处理-LocalDateUtil
于 2020-07-13 20:41:00 首次发布
本文详细介绍了Java8中LocalDateUtil类的使用方法,包括创建日期、日期比较、日期格式化等核心功能,帮助开发者更好地进行日期处理。


785

被折叠的 条评论
为什么被折叠?



