java.time.Period.between() 方法示例



描述

java.time.Period.between() 方法獲取表示兩個 LocalDate 物件之間的 Period。

宣告

以下是 java.time.Period.between() 方法的宣告。

public static Period between(LocalDate startInclusive, LocalDate endExclusive)

引數

  • startInclusive − 包含開始日期,非空。

  • endExclusive − 排除結束日期,非空。

返回值

非空的 Period。

示例

以下示例展示了 java.time.Period.between() 方法的用法。

package com.tutorialspoint;

import java.time.LocalDate;
import java.time.Period;

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

      Period period = Period.between(LocalDate.ofYearDay(2017, 200), 
         LocalDate.ofYearDay(2017, 300));

      System.out.println(period);  
   }
}

讓我們編譯並執行上述程式,這會產生以下結果 −

P3M8D
廣告