Java中的 LocalTime toSecondOfDay() 方法及示例
LocalTime类的toSecondOfDay()方法用于从该 LocalTime 返回时间为一天中的秒数。此方法返回的值介于 0 到 24 * 60 * 60 – 1 之间。
句法:
public int toSecondOfDay()
参数:此方法不接受任何参数。
返回值:此方法返回一个long 值,它是一天中的第二个值,相当于这个时间。
下面的程序说明了 toSecondOfDay() 方法:
方案一:
// Java program to demonstrate
// LocalTime.toSecondOfDay() 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 second of day from toSecondOfDay()
int value = time.toSecondOfDay();
// print result
System.out.println("Second of day: "
+ value);
}
}
输出:
Second of day: 70490
方案二:
// Java program to demonstrate
// LocalTime.toSecondOfDay() 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 Second of day from toSecondOfDay()
int value = time.toSecondOfDay();
// print result
System.out.println("Second of day: "
+ value);
}
}
输出:
Second of day: 84105
参考: https: Java/time/LocalTime.html#toSecondOfDay()