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



描述

java.time.Period.of(int years, int months, int days) 方法獲取表示指定單位中量的週期。

宣告

以下是 java.time.Period.of(int years, int months, int days) 的宣告。

public static Period of(int years, int months, int days)

引數

  • years - 年份,可以為負數。

  • months - 月份,可以為負數。

  • days - 天數,可以為負數。

返回值

週期,不為 null。

示例

以下示例演示了 java.time.Period.of(int years, int months, int days) 方法的使用。

package com.tutorialspoint;

import java.time.Period;

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

      Period period = Period.of(1,5,2);
      System.out.println("Years: " + period.getYears() 
         + ", Months: " + period.getMonths()
         +", Days: " + period.getDays());     
   }
}

讓我們編譯並執行以上程式,這將產生以下結果 -

Years: 1, Months: 5, Days: 2
廣告