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