Loading
0

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();

最后编辑于:2024/4/24作者: joycode

我不入地狱,谁入地狱?

评论已关闭