周期类的ofDays()方法用于从给定的天数中获取一个周期作为参数。该参数以整数形式接受。此方法返回具有给定天数的Period。
用法:
public static Period ofDays(int numberOfDays)
参数:此方法接受单个参数numberOfDays,该参数是要解析为Period对象的天数。
返回值:此函数返回以给定天数解析的Period对象。
下面是Period.ofDays()方法的实现:
示例1:
// Java code to demonstrate ofDays() method
import java.time.Period;
class GFG {
public static void main(String[] args)
{
// Get the number of Days
int numberOfDays = 5;
// Parse the numberOfDays into Period
// using ofDays() method
Period p = Period.ofDays(numberOfDays);
System.out.println(p.getYears() + " Years\n"
+ p.getMonths() + " Months\n"
+ p.getDays() + " Days");
}
}
输出:
0 Years
0 Months
5 Days
示例2:
// Java code to demonstrate ofDays() method
import java.time.Period;
class GFG {
public static void main(String[] args)
{
// Get the number of Days
int numberOfDays = -5;
// Parse the numberOfDays into Period
// using ofDays() method
Period p = Period.ofDays(numberOfDays);
System.out.println(p.getYears() + " Years\n"
+ p.getMonths() + " Months\n"
+ p.getDays() + " Days");
}
}
输出:
0 Years
0 Months
-5 Days