java.time包中的Duration Class的getUnits()方法用於獲取此持續時間支持的單位列表。
用法:
public List<TemporalUnit> getUnits()
參數:此方法不接受任何參數。
返回值:此方法返回此持續時間支持的單位列表。這些是SECONDS和NANOS。
以下示例說明了Duration.getUnits()方法:
示例1:
// Java code to illustrate getUnits() method
import java.time.Duration;
import java.time.temporal.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Get the text
String time = "P2DT3H4M";
// Duration using parse() method
Duration duration
= Duration.parse(time);
// Duration using getUnits() method
List<TemporalUnit>
listOfUnits = duration.getUnits();
System.out.println("List of Units: "
+ listOfUnits);
}
}
輸出:
List of Units: [Seconds, Nanos]
示例2:
// Java code to illustrate getUnits() method
import java.time.Duration;
import java.time.temporal.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Get the text
String time = "P1DT4M";
// Duration using parse() method
Duration duration
= Duration.parse(time);
// Duration using getUnits() method
List<TemporalUnit>
listOfUnits = duration.getUnits();
System.out.println("List of Units: "
+ listOfUnits);
}
}
輸出:
List of Units: [Seconds, Nanos]
參考: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#getUnits–
相關用法
- Java ChronoPeriod getUnits()用法及代碼示例
- Java Period getUnits()用法及代碼示例
- Java Duration minus(Duration)用法及代碼示例
- Java Duration compareTo(Duration)用法及代碼示例
- Java Duration equals(Duration)用法及代碼示例
- Java Duration dividedBy(Duration)用法及代碼示例
- Java Duration plus(Duration)用法及代碼示例
- Java Duration abs()用法及代碼示例
- Java Duration toMillis()用法及代碼示例
- Java Duration from(TemporalUnit)用法及代碼示例
- Java Duration toMinutes()用法及代碼示例
- Java Duration toNanos()用法及代碼示例
- Java Duration toDaysPart()用法及代碼示例
- Java Duration toSeconds()用法及代碼示例
- Java Duration toDays()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Duration getUnits() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。