package com.izkml.mlyun.uc.user.util;
import com.zkml.common.util.MyUtil;
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: 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;
}
}