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


Java ChronoUnit isTimeBased()用法及代碼示例


ChronoUnit枚舉的isTimeBased()方法用於檢查此ChronoUnit是否為時間單位。從nanos到half-days的所有ChronoUnit都是基於時間的。

用法:

public boolean isTimeBased()

參數:此方法不接受任何內容。


返回值:如果使用時間單位,則此方法返回true;如果使用數據單位,則返回false。

以下示例程序旨在說明ChronoUnit.isTimeBased()方法:
程序1:

// Java program to demonstrate 
// ChronoUnit.isTimeBased() method 
  
import java.time.temporal.ChronoUnit; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get ChronoUnit 
        ChronoUnit chronoUnit 
            = ChronoUnit.valueOf("NANOS"); 
  
        // apply isTimeBased() 
        boolean isTimeBasedAttribute 
            = chronoUnit.isTimeBased(); 
  
        // print 
        System.out.println( 
            "NANOS"
            + " is Date based attribute:"
            + isTimeBasedAttribute); 
    } 
}
輸出:
NANOS is Date based attribute:true

程序2:

// Java program to demonstrate 
// ChronoUnit.isTimeBased() method 
  
import java.time.temporal.ChronoUnit; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get ChronoUnit 
        ChronoUnit chronoUnit 
            = ChronoUnit.valueOf("DAYS"); 
  
        // apply isTimeBased() 
        boolean isTimeBasedAttribute 
            = chronoUnit.isTimeBased(); 
  
        // print 
        System.out.println( 
            "DAYS"
            + " is Date based attribute:"
            + isTimeBasedAttribute); 
    } 
}
輸出:
DAYS is Date based attribute:false

參考:https://docs.oracle.com/javase/10/docs/api/java/time/temporal/ChronoUnit.html#isTimeBased()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ChronoUnit isTimeBased() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。