Java中的 LocalTime plusSeconds() 方法及示例
LocalTime类的plusSeconds()方法用于将指定的秒数值添加到此 LocalTime 并将结果作为 LocalTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。
句法:
public LocalTime plusSeconds(long SecondsToAdd)
参数:此方法接受单个参数SecondsToAdd ,即要添加的 Seconds 的值,可以是负值。
返回值:此方法根据此时间返回一个LocalTime ,并添加 Seconds。
下面的程序说明了 plusSeconds() 方法:
方案一:
// Java program to demonstrate
// LocalTime.plusSeconds() 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");
// print LocalTime
System.out.println("LocalTime before addition: "
+ time);
// add 200 Seconds using plusSeconds()
LocalTime value = time.plusSeconds(200);
// print result
System.out.println("LocalTime after addition: "
+ value);
}
}
输出:
LocalTime before addition: 19:34:50.630
LocalTime after addition: 19:38:10.630
方案二:
// Java program to demonstrate
// LocalTime.plusSeconds() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("01:00:01");
// print LocalTime
System.out.println("LocalTime before addition: "
+ time);
// add -3400 Seconds using plusSeconds()
LocalTime value = time.plusSeconds(-3400);
// print result
System.out.println("LocalTime after addition: "
+ value);
}
}
输出:
LocalTime before addition: 01:00:01
LocalTime after addition: 00:03:21
参考资料: https: Java/time/LocalTime.html#plusSeconds(long)