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

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

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

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

句法:

public int getDays()

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

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

下面的程序说明了上述方法:

方案一:

// 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

方案二:

// 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: Java/time/Period.html#getDays–