Java 8 中 `LocalTime ofNanoOfDay()` 方法


可以使用 Java 8 中 `LocalTime` 類的 `ofNanoOfDay()` 方法以及一天的納秒值來獲取 `LocalTime` 物件。該方法需要一個引數,即一天的納秒值,並將返回該納秒值對應的 `LocalTime` 物件。

以下程式演示了這一過程:

示例

 線上演示

import java.time.*;
public class Demo {
   public static void main(String[] args){
      long nanoSeconds = 1000000000;
      System.out.println("The nanoseconds of the day: " + nanoSeconds);
      System.out.println("The LocalTime is: " + LocalTime.ofNanoOfDay(nanoSeconds));
   }
}

輸出

The nanoseconds of the day: 1000000000
The LocalTime is: 00:00:01

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

首先,顯示一天的納秒數。然後,使用 `ofNanoOfDay()` 方法獲取一天的納秒數對應的 `LocalTime` 物件,並對其進行列印。以下程式碼段演示了這一過程:

long nanoSeconds = 1000000000;
System.out.println("The nanoseconds of the day: " + nanoSeconds);
System.out.println("The LocalTime is: " + LocalTime.ofNanoOfDay(nanoSeconds));

更新於: 2019 年 7 月 30 日

82 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.