java.time包中的Duration Class的ofNanos(long)方法用於獲取1 nanoSecond格式的持續時間。
用法:
public static Duration ofNanos(long nanoSeconds)
參數:此方法接受參數nanoSeconds,即nanoSeconds的數量。它可以是正數或負數。
返回值:此方法以1 nanoSecond格式返回表示時間的Duration。
異常:如果輸入的nanoSeconds超過了Duration的容量,則此方法將引發ArithmeticException。
以下示例說明了Duration.ofNanos()方法:
示例1:
// Java code to illustrate ofNanos() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// input number of Nanos
long noOfNanos = 1000000;
// Duration using ofNanos() method
Duration duration
= Duration.ofNanos(noOfNanos);
System.out.println(duration.getSeconds());
}
}
輸出:
0
示例2:
// Java code to illustrate ofNanos() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// input number of Nanos
long noOfNanos = -1000000;
// Duration using ofNanos() method
Duration duration
= Duration.ofNanos(noOfNanos);
System.out.println(duration.getSeconds());
}
}
輸出:
-1
參考: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#ofNanos-long-
相關用法
- Java Duration compareTo(Duration)用法及代碼示例
- Java Duration dividedBy(Duration)用法及代碼示例
- Java Duration minus(Duration)用法及代碼示例
- Java Duration equals(Duration)用法及代碼示例
- Java Duration plus(Duration)用法及代碼示例
- Java Duration abs()用法及代碼示例
- Java Duration hashCode()用法及代碼示例
- Java Duration toString()用法及代碼示例
- Java Duration toNanos()用法及代碼示例
- Java Duration toMinutes()用法及代碼示例
- Java Duration toDaysPart()用法及代碼示例
- Java Duration toSeconds()用法及代碼示例
- Java Duration toDays()用法及代碼示例
- Java Duration negated()用法及代碼示例
- Java Duration from(TemporalUnit)用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Duration ofNanos(long) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。