Java 中的 Duration ZERO 欄位


ZERO 欄位將 Duration 類的持續時間設定為零。該欄位與 Java 中不同資料型別的 null 值完全相同。

一個演示 ZERO 欄位的程式如下 −

示例

 線上演示

import java.time.Duration;
public class Demo {
   public static void main(String[] args) {
      Duration d = Duration.ZERO;
      boolean flag = d.isZero();
      System.out.println("The duration is: " + d);
      if(flag)
         System.out.println("The above duration is of zero length");
      else
         System.out.println("The above duration is not of zero length");
   }
}

輸出

The duration is: PT0S
The above duration is of zero length

現在讓我們瞭解一下上面的程式。

首先使用 ZERO 域將持續時間設定為零,然後顯示它。然後標誌儲存 isZero() 方法的返回值。根據標誌中的值,持續時間的長度為零或非零並將列印。一個演示此功能的程式碼片段如下 −

Duration d = Duration.ZERO;
boolean flag = d.isZero();
System.out.println("The duration is: " + d);
if(flag)
System.out.println("The above duration is of zero length");
else
System.out.println("The above duration is not of zero length");

更新於:30-Jul-2019

470 次瀏覽

開啟你的 職業

完成課程獲取認證

開始
廣告