LocalDateTime `query()` 方法在 Java 中


LocalDateTime 物件可以使用 Java 中 LocalDateTime 類的 query 方法根據需要進行查詢。此方法需要一個引數,即要呼叫的查詢,並返回查詢的結果。

下面給出了展示此內容的程式 −

示例

 線上演示

import java.time.*;
import java.time.temporal.*;
public class Demo {
   public static void main(String[] args) {
      LocalDateTime ldt = LocalDateTime.parse("2019-02-18T23:15:30");
      System.out.println("The LocalDateTime is: " + ldt);
      String precision = ldt.query(TemporalQueries.precision()).toString();
      System.out.println("The Precision for the LocalDateTime is: "+ precision);
   }
}

輸出

The LocalDateTime is: 2019-02-18T23:15:30
The Precision for the LocalDateTime is: Nanos

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

首先顯示 LocalDateTime 物件。然後使用 query() 方法查詢 LocalDateTime 物件,並顯示查詢結果。下面給出的程式碼片段對此進行了說明 −

LocalDateTime ldt = LocalDateTime.parse("2019-02-18T23:15:30");
System.out.println("The LocalDateTime is: " + ldt);
String precision = ldt.query(TemporalQueries.precision()).toString();
System.out.println("The Precision for the LocalDateTime is: "+ precision);

更新時間: 2019 年 7 月 30 日

98 次檢視

開啟你的 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.