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


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


Java中的Period类的getDays()方法用于获取当前使用该期间的天数。

用法:

public int getDays()

参数:此方法不接受任何参数。


返回值:此函数返回给定期间的天数。

以下示例程序旨在说明上述方法:

示例1:

// Java code to show the function getDays() 
// to get number of days from given period 
import java.time.Period; 
import java.time.temporal.ChronoUnit; 
  
public class PeriodDemo { 
  
    // Function to get number of days of given period 
    static void getNumberOfDays(int year, int months, int days) 
    { 
        Period period = Period.of(year, months, days); 
        System.out.println(period.getDays()); 
    } 
  
    // Driver Code 
    public static void main(String[] args) 
    { 
  
        int year = 8; 
        int months = 5; 
        int days = 25; 
  
        getNumberOfDays(year, months, days); 
    } 
}
输出:
25

示例2:

// Java code to show the function getDays() 
// to get number of days from given period 
import java.time.Period; 
import java.time.temporal.ChronoUnit; 
  
public class PeriodDemo { 
  
    // Function to get number of days of given period 
    static void getNumberOfDays(int year, int months, int days) 
    { 
        Period period = Period.of(year, months, days); 
        System.out.println(period.getDays()); 
    } 
  
    // Driver Code 
    public static void main(String[] args) 
    { 
  
        int year = 0; 
        int months = 0; 
        int days = 365; 
  
        getNumberOfDays(year, months, days); 
    } 
}
输出:
365

参考: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#getDays–



相关用法


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