Java中ChronoPeriod接口的get()方法用於從此ChronoPeriod中獲取自變量中指定的請求單位(YEARS,MONTHS或DAYS)的值。
用法:
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.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
// Function to get requested unit
static void getUnit(int year, int months, int days)
{
ChronoPeriod 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.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
// Function to get requested unit
static void getUnit(int year, int months, int days)
{
ChronoPeriod 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 ChronoPeriod between()用法及代碼示例
- Java ChronoPeriod plus()用法及代碼示例
- Java ChronoPeriod hashCode()用法及代碼示例
- Java ChronoPeriod negated()用法及代碼示例
- Java ChronoPeriod equals()用法及代碼示例
- Java ChronoPeriod negated()用法及代碼示例
- Java ChronoPeriod addTo()用法及代碼示例
- Java ChronoPeriod subtractFrom()用法及代碼示例
- Java ChronoPeriod getChronology()用法及代碼示例
- Java ChronoPeriod minus()用法及代碼示例
- Java ChronoPeriod getUnits()用法及代碼示例
- Java ChronoPeriod toString()用法及代碼示例
- Java ChronoPeriod isZero()用法及代碼示例
- Java ChronoPeriod isNegative()用法及代碼示例
- Java ChronoPeriod multipliedBy()用法及代碼示例
注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoPeriod get() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。