LocalDateTime与Date间的转换

Date转LocalDateTime:

Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());

LocalDateTime转Date:

LocalDateTime ldt=LocalDateTime.now();
Date freezeTime = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());

获取时间戳:

LocalDateTime currentTime = LocalDateTime.now();
Instant timestamp = currentTime.atZone(ZoneId.systemDefault()).toInstant();
long timestampMillis = timestamp.toEpochMilli();
LocalDateTime currentTime = LocalDateTime.now();
long timestampMillis = currentTime.toInstant(ZoneOffset.UTC).toEpochMilli();
LocalDateTime currentTime = LocalDateTime.now();
Duration duration = Duration.between(LocalDateTime.EPOCH, currentTime);
long timestampMillis = duration.toMillis();
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享