LocalDatetime 与 Date、timestamp 互相转化
一、概述:
在Java中可以使用Date,Calender或者是LocalDatetime进行时间日期的处理,Java8新推出的LocalDatetime类由于线程安全、简单、高可靠而获得广泛使用。
二、代码示例:
2.1,LocalDateTime转时间戳
public static Long localDateTimeToTimestamp(LocalDateTime localDateTime) {
try {
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zoneId).toInstant();
return instant.toEpochMilli();//毫秒时间戳
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2.2 ,时间戳转LocalDateTime
public static LocalDateTime timestampToLocalDateTime(long timestamp) {
try {
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
} catch