Java中的 LocalTime minusSeconds() 方法及示例
LocalTime类的minusSeconds()方法用于从此 LocalTime 中减去指定的 Seconds 值,并将结果作为 LocalTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。
句法:
public LocalTime minusSeconds(long SecondsToSubtract)
参数:该方法接受单个参数SecondsToSubtract ,它是要减去的秒数,可以是负值。
返回值:此方法返回基于此时间减去 Seconds 的LocalTime 。
下面的程序说明了 minusSeconds() 方法:
方案一:
// Java program to demonstrate
// LocalTime.minusSeconds() 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 subtraction: "
+ time);
// subtract 200 Seconds using minusSeconds()
LocalTime value = time.minusSeconds(200);
// print result
System.out.println("LocalTime after subtraction: "
+ value);
}
}
输出:
LocalTime before subtraction: 19:34:50.630
LocalTime after subtraction: 19:31:30.630
方案二:
// Java program to demonstrate
// LocalTime.minusSeconds() 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 subtraction: "
+ time);
// subtract -3400 Seconds using minusSeconds()
LocalTime value = time.minusSeconds(-3400);
// print result
System.out.println("LocalTime after subtraction: "
+ value);
}
}
输出:
LocalTime before subtraction: 01:00:01
LocalTime after subtraction: 01:56:41
参考资料: https: Java/time/LocalTime.html#minusSeconds(long)