当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Period ofDays()用法及代码示例


周期类的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-



相关用法


注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 Period ofDays() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。