文章目录[隐藏]
- 1 与Date互相转换
- 2 与时间戳互相转换
- 3 时间区间 秒转天时分
https://www.yiibai.com/javatime/javatime_localdate.html
1 与Date互相转换
Localdate转date
Date date = Date.from(planDate.atStartOfDay(ZoneId.systemDefault()).toInstant()
LocalDateTime转date
Date date = Date.from( localDateTime.atZone(ZoneId.systemDefault()).toInstant());
date转LocalDate
LocalDate localDate = LocalDate.ofInstant(date.toInstant(),
ZoneId.systemDefault());
date转LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(),
ZoneId.systemDefault());
2 与时间戳互相转换
- 时间戳转 13位 LocalDateTime
LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault());
- 时间戳转 10位 LocalDateTime
LocalDateTime.ofInstant(Instant.ofEpochSecond(time),ZoneId.systemDefault());
3 时间区间 秒转天时分
代码语言:javascript复制# 运行时间
Duration duration = Duration.ofSeconds(uptime);
String uptimeNew = duration.toDays() "天" duration.toHoursPart() "小时" duration.toMinutesPart() "分钟";