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

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

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

Period 类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

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/Period.html#ofDays-int-