当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Duration negated()用法及代码示例


java.time包中的Duration Class的negated()方法用于获取该持续时间为不可变的不变副本。

用法:

public Duration negated()

参数:此方法不接受任何参数。


返回值:此方法返回Duration,它是现有持续时间的不变副本,且持续时间取反。

异常:如果发生数字溢出,则此方法将引发ArithmeticException。

以下示例说明了Duration.negated()方法:

示例1:

// Java code to illustrate negated() method 
  
import java.time.Duration; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Duration 1 using parse() method 
        Duration duration1 
            = Duration.parse("P2DT3H4M"); 
  
        // Get the duration negated using negated() method 
        System.out.println(duration1.negated()); 
    } 
}
输出:
PT-51H-4M

示例2:

// Java code to illustrate negated() method 
  
import java.time.Duration; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Duration 
        Duration duration2 
            = Duration.ofDays(5); 
  
        // Get the duration negated 
        // using negated() method 
        System.out.println(duration2.negated()); 
    } 
}
输出:
PT-120H

参考: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#negated–



相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Duration negated() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。