集册 Java实例教程 找出两个日期之间的时间段

找出两个日期之间的时间段

欢马劈雪     最近更新时间:2020-01-02 10:19:05

501
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
找出两个日期之间的时间段

import java.time.LocalDate;

import java.time.Month;/*来自 nowjava.com*/

import java.time.Period;


public class Main {


  public static void main(String[] args) {

    LocalDate anniversary = LocalDate.of(2000, Month.NOVEMBER, 11);

    LocalDate today = LocalDate.now();

    Period period = Period.between(anniversary, today);

    System.out.println("Number of Days Difference: " +  period.getDays());

    System.out.println("Number of Months Difference: " + period.getMonths());

    System.out.println("Number of Years Difference: " + period.getYears());

  }

}