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


Java Duration withSeconds(long)用法及代码示例


java.time包中的Duration Class的withSeconds(long)方法用于获取此持续时间的不可变副本,并将指定的秒数作为参数传递。

用法:

public Duration withSeconds(long amountOfSeconds)

参数:此方法接受参数amountOfSeconds,即秒数。


返回值:此方法返回作为参数传递的秒数的持续时间。

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

示例1:

// Java code to illustrate withSeconds() method 
  
import java.time.Duration; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the amount of seconds 
        long amountOfSeconds = 300; 
  
        // Duration using parse() method 
        Duration duration 
            = Duration.parse("P2DT3H4M"); 
  
        // Get the duration in seconds 
        // using withSeconds() method 
        System.out.println(duration 
                               .withSeconds(amountOfSeconds)); 
    } 
}
输出:
PT5M

示例2:

// Java code to illustrate withSeconds() method 
  
import java.time.Duration; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the amount of seconds 
        long amountOfSeconds = 3000; 
  
        // Duration using ofHours() method 
        Duration duration 
            = Duration.ofHours(5); 
  
        // Get the duration in seconds 
        // using withSeconds() method 
        System.out.println(duration 
                               .withSeconds(amountOfSeconds)); 
    } 
}
输出:
PT50M

参考: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#withSeconds-long-



相关用法


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