可以使用Java的Period类中的of()方法以给定的天数,月数和年数获取Period。此方法需要3个参数,即天数,月数和年数。而且,它返回具有给定天数,月数和年数的Period对象。
演示此程序如下
示例import java.time.Period;
public class Demo {
public static void main(String[] args) {
int days = 20;
int months = 11;
int years = 3;
Period p = Period.of(years, months, days);
System.out.println("The Period is: " + p);
}
}
输出结果The Period is: P3Y11M20D
现在让我们了解上面的程序。
使用of()方法获取给定天数,月数和年数的期间,然后将其打印出来。演示此代码段如下所示:int days = 20;
int months = 11;
int years = 3;
Period p = Period.of(years, months, days);
System.out.println("The Period is: " + p);