📜  Java中的 Period ofWeeks() 方法及示例

📅  最后修改于: 2022-05-13 01:54:59.107000             🧑  作者: Mango

Java中的 Period ofWeeks() 方法及示例

Period 类ofWeeks()方法用于从给定的周数中获取周期作为参数。此参数以整数形式接受。此方法返回具有给定周数的 Period。

句法:

public static Period ofWeeks(int numberOfWeeks)

参数:此方法接受单个参数numberOfWeeks ,它是要解析为 Period 对象的周数。

返回:此函数返回周期,它是使用给定周数解析的 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-