Java中的LocalTime ofNanoOfDay()方法

可以使用ofNanoOfDay()Java中LocalTime类中的方法,使用一天中的纳秒来获取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));