當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Duration ofDays()用法及代碼示例

Duration類ofDays()方法

  • ofDays() 方法可在java.time包。
  • ofDays() 方法用於表示此 Duration 中的給定天數。
  • ofDays() 方法是一個靜態方法,它可以通過類名訪問,如果我們嘗試使用類對象訪問該方法,那麽我們不會得到錯誤。
  • ofDays() 方法可能在表示天的時候拋出異常。
    ArithmeticException:當給定日期的值超過此 Duration 的長度時,可能會引發此異常。

用法:

    public static Duration ofDays(long day_val);

參數:

  • long day_val– 表示價值的天數。

返回值:

這個方法的返回類型是Duration,它返回保存給定天數的持續時間。

例:

// Java program to demonstrate the example 
// of ofDays(long day_val) method 
// of Duration

import java.time.*;
import java.time.temporal.*;

public class OfDaysOfDuration {
    public static void main(String args[]) {
        long days_val = 3;

        // returns the Duration that holds 
        // the value of the given argument i.e. 
        // here we are representing 3 days (72H) 
        // in this Duration du1
        Duration du1 = Duration.ofDays(days_val);

        // Display du1
        System.out.println("Duration.ofDays(3):" + du1);
    }
}

輸出

Duration.ofDays(3):PT72H


相關用法


注:本文由純淨天空篩選整理自 Java Duration Class | ofDays() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。