Java 中的 Period ofYears() 方法
可以使用 Java 中 `Period` 類中的 `ofYears()` 方法獲取給定年數的時間段。此方法需要一個引數,即年數,並返回具有給定年數的時間段物件。
以下是演示的程式 −
示例
import java.time.Period;
public class Demo {
public static void main(String[] args) {
int years = 3;
Period p = Period.ofYears(years);
System.out.println("The Period is: " + p);
}
}輸出
The Period is: P3Y
現在讓我們瞭解上述程式。
使用 `ofYears()` 方法獲取指定年數的時間段,然後列印。以下程式碼片段演示了這一點 −
int years = 3;
Period p = Period.ofYears(years);
System.out.println("The Period is: " + p);
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP