top.doudou.common.tool.utils.DateTimeUtil Maven / Gradle / Ivy
package top.doudou.common.tool.utils;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import top.doudou.base.util.StrUtils;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* LocalDateTime与DateTime转换
* @Author 傻男人 <[email protected]>
* @Date 2020-09-22 9:21
* @Version V1.0
*/
@Slf4j
public final class DateTimeUtil {
/**
* 将Date转换为 LocalDateTime
*
* @param date
* @return
*/
public static synchronized LocalDateTime date2LocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
log.debug("当前时区为 {}",zone);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime;
}
/**
* 将LocalDateTime转换为 Date
*
* @param localDateTime
* @return
*/
public static synchronized Date localDateTime2Date(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
log.debug("当前时区为 {}",zone);
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
return date;
}
/**
* 获取现在时间的字符串 yyyyMMdd
* @return
*/
public static String getThisDateTime(){
return DateUtil.thisYear()+ StrUtils.cover(DateUtil.thisMonth()+1,2)+StrUtils.cover(DateUtil.thisDayOfMonth(),2);
}
}