Java中ChronoPeriod類的getUnits()方法用於獲取此ChronoPeriod支持的單位集。列表中支持的單位是YEARS,MONTHS,DAYS(僅按此順序)。
用法:
List getUnits()
參數:此方法不接受任何參數。
返回值此方法返回包含年,月和日的列表。
以下示例程序旨在說明上述方法:
程序1:
// Java code to show the function getUnits()
// to get the set of units supported by period
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
// Function to get the set of units supported by period
static void getNumberOfDays(int year, int months, int days)
{
ChronoPeriod period = Period.of(year, months, days);
System.out.println(period.getUnits());
}
// Driver Code
public static void main(String[] args)
{
int year = 1;
int months = 13;
int days = 36;
getNumberOfDays(year, months, days);
}
}
輸出:
[Years, Months, Days]
程序2:
// Java code to show the function getUnits()
// to get the set of units supported by period
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
// Function to get the set of units supported by period
static void getNumberOfDays(int year, int months, int days)
{
ChronoPeriod period = Period.ofDays(days);
System.out.println(period.getUnits());
}
// Driver Code
public static void main(String[] args)
{
int year = 0;
int months = 0;
int days = 0;
getNumberOfDays(year, months, days);
}
}
輸出:
[Years, Months, Days]
參考: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#getUnits–
相關用法
- Java Duration getUnits()用法及代碼示例
- Java Period getUnits()用法及代碼示例
- Java ChronoPeriod get()用法及代碼示例
- Java ChronoPeriod between()用法及代碼示例
- Java ChronoPeriod plus()用法及代碼示例
- Java ChronoPeriod negated()用法及代碼示例
- Java ChronoPeriod minus()用法及代碼示例
- Java ChronoPeriod isNegative()用法及代碼示例
- Java ChronoPeriod addTo()用法及代碼示例
- Java ChronoPeriod hashCode()用法及代碼示例
- Java ChronoPeriod subtractFrom()用法及代碼示例
- Java ChronoPeriod isZero()用法及代碼示例
- Java ChronoPeriod getChronology()用法及代碼示例
- Java ChronoPeriod negated()用法及代碼示例
- Java ChronoPeriod multipliedBy()用法及代碼示例
注:本文由純淨天空篩選整理自srinam大神的英文原創作品 ChronoPeriod getUnits() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。