LocalDate,LocalDateTime计算两个时间的相差时间

LcoalDateTime


LocalDateTime now = LocalDateTime.now();
LocalDateTime end = LocalDateTime.now();

Duration duration = Duration.between(now,end);
//相差的天数
long days = duration.toDays();
//相差的小时数
long hours = duration.toHours();
//相差的分钟数
long minutes = duration.toMinutes();
//相差毫秒数
long millis = duration.toMillis();
//相差的纳秒数
long nanos = duration.toNanos();

LocalDate

LocalDate start = LocalDate.of(2020, 12, 27);
Period period = Period.between(start, LocalDate.now());
// 日期相差
int days = period.getDays();
// 相差月份
int months = period.getMonths();
// 相差年份
int years = period.getYears();
System.out.println(days +"-"+ months +"-"+ years);
// 更准确 - 开发用这个,省事
long dayse = LocalDate.now().toEpochDay() - start.toEpochDay();
System.out.println("相差天数" + dayse);
posted @ 2021-12-29 11:13  InkYi  阅读(6181)  评论(0编辑  收藏  举报