Java-LocalDate的使用| #日期差值#
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { LocalDate date1 = LocalDate.parse(scanner.next(), DateTimeFormatter.BASIC_ISO_DATE); LocalDate date2 = LocalDate.parse(scanner.next(), DateTimeFormatter.BASIC_ISO_DATE); System.out.println(date1.until(date2, ChronoUnit.DAYS) + 1); } } }
官方文档:[https://www.runoob.com/manual/jdk11api/java.base/java/time/LocalDate.html]
LocalDate.parse(string,formatter)将字符串按照指定模式格式化为LocalDate对象。