1. 计算LocalDate之间的天数差的方法
public static Long claculateGapBetweenTwoLocalDate(LocalDate localDateStart, LocalDate localDateEnd) { Duration duration = Duration.between(localDateStart.atStartOfDay(), localDateEnd.atStartOfDay()); return duration.toDays(); }
2. 计算过程及结果示例
- 可以使用方法将字符串转为
LocalDate
:
public static LocalDate transferString2LocalDate(String s) { DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd"); return LocalDate.parse(s, fmt); }
2.以2022-06-01
至2022-06-03
为例:
public static void main(String[] args) { LocalDate start = transferString2LocalDate("2022-06-01"); LocalDate end = transferString2LocalDate("2022-06-03"); System.out.println(claculateGapBetweenTwoLocalDate(start, end)); }
3.运行结果
2