📌  相关文章
📜  Java中的 ChronoPeriod getUnits() 方法及示例

📅  最后修改于: 2022-05-13 01:55:39.706000             🧑  作者: Mango

Java中的 ChronoPeriod getUnits() 方法及示例

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]

方案二

// 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–