Java中Period類的get()方法用於獲取此Period的自變量中給出的請求單位(YEARS,MONTHS或DAYS)的值。
用法:
public long get(TemporalUnit unit)
參數:此方法接受類型為TemporalUnit的單個參數uint,這是獲取所需單位的單位。
返回值:此函數返回所請求單位的long值。
異常:
- DateTimeException-如果不支持參數中的單位,則此方法將引發DateTimeException。
- UnsupportedTemporalTypeException-如果不支持參數中給定的單位,則此方法將引發UnsupportedTemporalTypeException。
以下示例程序旨在說明上述方法:
程序1:
// Java code to show the function get()
// which gives the requested unit
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to get requested unit
static void getUnit(int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.get(ChronoUnit.DAYS));
}
// Driver Code
public static void main(String[] args)
{
int year = 8;
int months = 5;
int days = 25;
getUnit(year, months, days);
}
}
輸出:
25
程序2:
// Java code to show the function get()
// which gives the requested unit
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to get requested unit
static void getUnit(int year, int months, int days)
{
Period period = Period.of(year, months, days);
System.out.println(period.get(ChronoUnit.YEARS));
}
// Driver Code
public static void main(String[] args)
{
int year = 11;
int months = 3;
int days = 21;
getUnit(year, months, days);
}
}
輸出:
11
相關用法
- Java Period from()用法及代碼示例
- Java Period of()用法及代碼示例
- Java Period between()用法及代碼示例
- Java Period plus()用法及代碼示例
- Java Period plusMonths()用法及代碼示例
- Java Period equals()用法及代碼示例
- Java Period getMonths()用法及代碼示例
- Java Period ofDays()用法及代碼示例
- Java Period ofWeeks()用法及代碼示例
- Java Period ofMonths()用法及代碼示例
- Java Period parse()用法及代碼示例
- Java Period withMonths()用法及代碼示例
- Java Period withDays()用法及代碼示例
- Java Period toTotalMonths()用法及代碼示例
- Java Period getDays()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Period get() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。