Java中的 LocalTime toNanoOfDay() 方法及示例
LocalTime类的toNanoOfDay()方法用于从该 LocalTime 返回时间的纳秒。此方法返回的值介于 0 到 24 * 60 * 60 * 1、000、000、000 – 1 之间。
句法:
public long toNanoOfDay()
参数:此方法不接受任何参数。
返回值:该方法返回一个long值,它是天的nano值,相当于这个时间。
下面的程序说明了 toNanoOfDay() 方法:
方案一:
// Java program to demonstrate
// LocalTime.toNanoOfDay() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("19:34:50.63");
// extract nano of day from toNanoOfDay()
long value = time.toNanoOfDay();
// print result
System.out.println("Nano of day: "
+ value);
}
}
输出:
Nano of day: 70490630000000
方案二:
// Java program to demonstrate
// LocalTime.toNanoOfDay() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("23:21:45.98");
// extract nano of day from toNanoOfDay()
long value = time.toNanoOfDay();
// print result
System.out.println("Nano of day: "
+ value);
}
}
输出:
Nano of day: 84105980000000
参考: https: Java/time/LocalTime.html#toNanoOfDay()