Java中的Period的getYears()方法用於獲取該周期使用的年數。
注意:15個月不等於1年3個月。
用法:
public List getYears()
參數:此方法不接受任何參數。
返回值:此方法返回當前期間的年數。
以下示例程序旨在說明上述方法:
程序1:
// Java code to show the function getYears()
// to get the number of years in the period
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to get the number of years
static void getNumberOfDays(int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.getYears());
}
// Driver Code
public static void main(String[] args)
{
int year = 12;
int months = 3;
int days = 31;
getNumberOfDays(year, months, days);
}
}
輸出:
12
程序2:
// Java code to show the function getYears()
// to get the number of years in the period
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to get the number of years
static void getNumberOfDays(int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.getYears());
}
// Driver Code
public static void main(String[] args)
{
int year = -12;
int months = 3;
int days = 31;
getNumberOfDays(year, months, days);
}
}
輸出:
-12
參考: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#getYears–
相關用法
- Java Period plus()用法及代碼示例
- Java Period of()用法及代碼示例
- Java Period between()用法及代碼示例
- Java Period get()用法及代碼示例
- Java Period from()用法及代碼示例
- Java Period ofDays()用法及代碼示例
- Java Period minusDays()用法及代碼示例
- Java Period parse()用法及代碼示例
- Java Period minus()用法及代碼示例
- Java Period ofMonths()用法及代碼示例
- Java Period minusMonths()用法及代碼示例
- Java Period isZero()用法及代碼示例
- Java Period hashCode()用法及代碼示例
- Java Period getMonths()用法及代碼示例
- Java Period equals()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Period getYears() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。