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-