首页javadateJava Data Type - 如何在两个LocalDate之间获取天,月和年

Java Data Type - 如何在两个LocalDate之间获取天,月和年

我们想知道如何在两个LocalDate之间获取天,月和年。
import java.time.LocalDate;
import java.time.Period;

public class Main {
  public static void main(String[] args) {

    LocalDate firstDate = LocalDate.of(2013, 5, 17); 
    LocalDate secondDate = LocalDate.of(2015, 3, 7); 
    Period period = Period.between(firstDate, secondDate);

    System.out.println(period);
    
    int days = period.getDays(); // 18
    
    int months = period.getMonths(); // 9
    int years = period.getYears(); // 4
    boolean isNegative = period.isNegative(); // false
    
    
  }
}