java.time包中的Duration Class的dividualBy(long)方法用於獲取此持續時間的不變副本除以作為參數傳遞的值。
用法:
public Duration dividedBy(long divisor)
參數:此方法接受參數除數,它是要除的值。它可以是正值或負值。
返回值:此方法返回一個Duration,它是現有持續時間的不變副本,參數值除以該持續時間。
異常:如果發生數字溢出,則此方法將引發ArithmeticException。
以下示例說明了Duration.dividedBy()方法:
示例1:
// Java code to illustrate dividedBy() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration 1 using parse() method
Duration duration1
= Duration.parse("P2DT3H4M");
// divisor value
long divisor = 2;
// Get the duration divided
// using dividedBy() method
System.out.println(duration1
.dividedBy(divisor));
}
}
輸出:
PT25H32M
示例2:
// Java code to illustrate dividedBy() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration 1 using parse() method
Duration duration1
= Duration.parse("P2DT3H4M");
// divisor value
long divisor = 10;
// Get the duration divided
// using dividedBy() method
System.out.println(duration1
.dividedBy(divisor));
}
}
輸出:
PT5H6M24S
參考:Oracle文檔
相關用法
- 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 dividedBy(long) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。