LocalTime類的minusSeconds()方法用於從此LocalTime中減去指定的秒數,然後將結果作為LocalTime對象返回。這一瞬間是一成不變的。計算大約在午夜結束。
用法:
public LocalTime minusSeconds(long SecondsToSubtract)
參數:此方法接受單個參數SecondsToSubtract,該參數是要減去的Seconds的值,它可以是負值。
返回值:此方法基於此時間返回LocalTime並減去Seconds。
以下示例程序旨在說明minusSeconds()方法:
示例1:
// 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
示例2:
// 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://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#minusSeconds(long)
相關用法
- Java OffsetTime minusSeconds()用法及代碼示例
- Java ZonedDateTime minusSeconds()用法及代碼示例
- Java Instant minusSeconds()用法及代碼示例
- Java OffsetDateTime minusSeconds()用法及代碼示例
- Java Duration minusSeconds(long)用法及代碼示例
- Java LocalTime from()用法及代碼示例
- Java LocalTime until()用法及代碼示例
- Java LocalTime plus()用法及代碼示例
- Java LocalTime with()用法及代碼示例
- Java LocalTime get()用法及代碼示例
- Java LocalTime withMinute()用法及代碼示例
- Java LocalTime withSecond()用法及代碼示例
- Java LocalTime range()用法及代碼示例
- Java LocalTime plusHours()用法及代碼示例
- Java LocalTime withNano()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LocalTime minusSeconds() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。