Java中Period类的getUnits()方法用于获取此Period支持的单位集。列表中支持的单位是YEARS,MONTHS,DAYS(仅按此顺序)。
用法:
public List getUnits()
参数:此方法不接受任何参数。
返回值此方法返回包含年,月和日的列表。
以下示例程序旨在说明上述方法:
程序1:
// Java code to show the function getUnits()
// to get the set of units supported by period
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to get the set of units supported by period
static void getNumberOfDays(int year, int months, int days)
{
Period 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.Period;
import java.time.temporal.ChronoUnit;
public class PeriodDemo {
// Function to get the set of units supported by period
static void getNumberOfDays(int year, int months, int days)
{
Period 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/8/docs/api/java/time/Period.html#getUnits–
相关用法
- Java ChronoPeriod getUnits()用法及代码示例
- Java Duration getUnits()用法及代码示例
- Java Period of()用法及代码示例
- Java Period from()用法及代码示例
- Java Period get()用法及代码示例
- Java Period plus()用法及代码示例
- Java Period between()用法及代码示例
- Java Period plusDays()用法及代码示例
- Java Period getYears()用法及代码示例
- Java Period hashCode()用法及代码示例
- Java Period isNegative()用法及代码示例
- Java Period isZero()用法及代码示例
- Java Period minusYears()用法及代码示例
- Java Period getDays()用法及代码示例
- Java Period multipliedBy()用法及代码示例
注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 Period getUnits() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。