Instant類的query()方法,用於使用指定的查詢作為參數來查詢此瞬間。作為參數傳遞的TemporalQuery對象定義了用於從該瞬間獲取結果的邏輯。
用法:
public <R> R query(TemporalQuery<R> query)
參數:此方法僅接受一個要調用的參數查詢。
返回值:此方法返回查詢結果,可能返回null。
異常:此方法引發以下異常:
- DateTimeException–如果無法查詢。
- ArithmeticException–如果發生數字溢出。
以下示例程序旨在說明query()方法:
示例1:
// Java program to demonstrate 
// Instant.query() method 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create Instant object 
        Instant instant 
            = Instant.parse("2018-12-31T10:15:30.00Z"); 
  
        // apply query method of Instant class 
        String value 
            = instant.query(TemporalQueries.precision()) 
                  .toString(); 
  
        // print the result 
        System.out.println("Precision value for Instant is "
                           + value); 
    } 
}
輸出:
Precision value for Instant is Nanos
示例2:顯示如果查詢未找到所需的對象,則返回null。
// Java program to demonstrate 
// Instant.query() method 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create Instant object 
        Instant instant 
            = Instant.parse("2018-12-31T10:15:30.00Z"); 
  
        // apply query method of Instant class 
        // print the result 
        System.out.println("Zone value for Instant is "
                           + instant.query(TemporalQueries.offset())); 
    } 
}
輸出:
Zone value for Instant is null
相關用法
- Java Instant until()用法及代碼示例
- Java Instant now()用法及代碼示例
- Java Instant from()用法及代碼示例
- Java Instant get()用法及代碼示例
- Java Instant with()用法及代碼示例
- Java Instant plus()用法及代碼示例
- Java Instant getLong()用法及代碼示例
- Java Instant compareTo()用法及代碼示例
- Java Instant equals()用法及代碼示例
- Java Instant ofEpochSecond()用法及代碼示例
- Java 8 Clock instant()用法及代碼示例
- Java Instant getNano()用法及代碼示例
- Java Instant isSupported()用法及代碼示例
- Java Instant minusMillis()用法及代碼示例
- Java Instant minusSeconds()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Instant query() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
