📜  Java中的 LocalTime ofNanoOfDay() 方法及示例

📅  最后修改于: 2022-05-13 01:54:50.983000             🧑  作者: Mango

Java中的 LocalTime ofNanoOfDay() 方法及示例

LocalTimeofNanoOfDay()方法用于从 nanos-of-day 值获取 LocalTime Instance。这将返回具有指定天数的 LocalTime,从 0 到 24 * 60 * 60 * 1, 000, 000, 000 – 1 作为参数传递。

句法:

public static LocalTime ofNanoOfDay(long nanoOfDay)

参数:此方法接受单个参数nanoOfDay ,它是一天的纳米,从 0 到 24 * 60 * 60 * 1, 000, 000, 000 – 1。

返回值:此方法返回从作为参数传递的 nanoOfDay 创建的 LocalTime实例

异常:如果作为参数传递的 nanoofDay 值无效,则此方法抛出DateTimeException

下面的程序说明了 ofNanoOfDay() 方法:

方案一:

// Java program to demonstrate
// LocalTime.ofNanoOfDay() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a nano of day
        long nanoofday = 14245000000000l;
  
        // applying ofNanoOfDay()
        LocalTime value
            = LocalTime.ofNanoOfDay(nanoofday);
  
        // print result
        System.out.println("LocalTime: "
                           + value);
    }
}
输出:
LocalTime: 03:57:25

方案二:

// Java program to demonstrate
// LocalTime.ofNanoOfDay() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a nano of day
        long nanoofday = 5000000000l;
  
        // applying ofNanoOfDay()
        LocalTime value
            = LocalTime.ofNanoOfDay(nanoofday);
  
        // print result
        System.out.println("LocalTime: "
                           + value);
    }
}
输出:
LocalTime: 00:00:05

参考: