Java Period ofWeeks()方法及示例
Period类 的 ofWeeks() 方法用于从给定的周数中获取一个周期作为参数。该参数以整数形式接受。该方法返回一个具有给定周数的周期。
语法
public static Period ofWeeks(int numberOfWeeks)
参数: 该方法接受一个参数 numberOfWeeks ,该参数是要被解析成Period对象的周数。
返回: 该函数返回周期,即用给定周数解析的周期对象。
下面是Period.ofWeeks()方法的实现。
例1:
// Java code to demonstrate ofWeeks() method
import java.time.Period;
class GFG {
public static void main(String[] args)
{
// Get the number of Weeks
int numberOfWeeks = 5;
// Parse the numberOfWeeks into Period
// using ofWeeks() method
Period p = Period.ofWeeks(numberOfWeeks);
System.out.println(p.getYears() + " Years\n"
+ p.getMonths() + " Months\n"
+ p.getDays() + " Days");
}
}
输出:
0 Years
0 Months
35 Days
例2:
// Java code to demonstrate ofWeeks() method
import java.time.Period;
class GFG {
public static void main(String[] args)
{
// Get the number of Weeks
int numberOfWeeks = -5;
// Parse the numberOfWeeks into Period
// using ofWeeks() method
Period p = Period.ofWeeks(numberOfWeeks);
System.out.println(p.getYears() + " Years\n"
+ p.getMonths() + " Months\n"
+ p.getDays() + " Days");
}
}
输出:
0 Years
0 Months
-35 Days
参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#ofWeeks-int-