Java ChronoPeriod getUnits()方法及示例

Java ChronoPeriod getUnits()方法及示例

Java中 ChronoPeriod类getUnits()方法 用于获取该ChronoPeriod所支持的单位集。支持的单位有年、月、日,并列在一个列表中(只按这个顺序)。

语法

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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程