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


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


java.time包中的Duration Class的toDaysPart()方法用于通过将秒数除以86400得到以天数为单位的该持续时间的值。

用法:

public long toDaysPart()

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


返回值:此方法返回一个long值,该值是此持续时间中的天数,方法是将秒数除以86400。

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

示例1:

// Java code to illustrate toDaysPart() method 
  
import java.time.Duration; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Duration using parse() method 
        Duration duration 
            = Duration.parse("P2DT3H4M"); 
  
        System.out.println("Duration: "
                           + duration); 
  
        // Get the number of days 
        // using toDaysPart() method 
        System.out.println(duration.toDaysPart()); 
    } 
}
输出:
Duration: PT51H4M
2
183840

示例2:

// Java code to illustrate toDaysPart() method 
  
import java.time.Duration; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Duration using ofDays() method 
        Duration duration 
            = Duration.ofDays(10); 
  
        System.out.println("Duration: "
                           + duration); 
  
        // Get the number of days 
        // using toDaysPart() method 
        System.out.println(duration.toDaysPart()); 
    } 
}
输出:
Duration: PT240H
10

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



相关用法


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