當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ZonedDateTime minusSeconds()用法及代碼示例


ZonedDateTime類的minusSeconds()方法用於從此ZonedDateTime中減去秒數,並在減法後返回ZonedDateTime的副本。此方法在即時時間軸上起作用,並且減去one-second則返回前一秒的時間軸。請注意,此方法與幾天,幾個月和幾年使用的方法不同。此實例是不可變的,不受此方法調用的影響。

用法:

public ZonedDateTime minusSeconds(long seconds)

參數:此方法接受單個參數seconds,該參數代表要減去的秒,它可以為負。


返回值:此方法基於此日期時間並減去秒,返回ZonedDateTime。

異常:如果結果超出支持的日期範圍,則此方法將引發DateTimeException。

以下示例程序旨在說明minusSeconds()方法:
示例1:

// Java program to demonstrate 
// ZonedDateTime.minusSeconds() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ZonedDateTime object 
        ZonedDateTime zoneddatetime 
            = ZonedDateTime.parse( 
                "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]"); 
  
        // print instance 
        System.out.println("ZonedDateTime before"
                           + " subtracting seconds: "
                           + zoneddatetime); 
  
        // subtract 100 seconds 
        ZonedDateTime returnvalue 
            = zoneddatetime.minusSeconds(100); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + " subtracting 100 seconds: "
                           + returnvalue); 
    } 
}
輸出:

ZonedDateTime before subtracting seconds: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after subtracting 100 seconds: 2018-12-06T19:19:32.123+05:30[Asia/Calcutta]

示例2:

// Java program to demonstrate 
// ZonedDateTime.minusSeconds() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ZonedDateTime object 
        ZonedDateTime zoneddatetime 
            = ZonedDateTime.parse( 
                "2018-10-25T23:12:31.123+02:00[Europe/Paris]"); 
  
        // print instance 
        System.out.println("ZonedDateTime before"
                           + " subtracting seconds: "
                           + zoneddatetime); 
  
        // subtract -22 seconds 
        ZonedDateTime returnvalue 
            = zoneddatetime.minusSeconds(-22); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + " subtracting -22 seconds: "
                           + returnvalue); 
    } 
}
輸出:

ZonedDateTime before subtracting seconds: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
ZonedDateTime after subtracting -22 seconds: 2018-10-25T23:12:53.123+02:00[Europe/Paris]

參考: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#minusSeconds(long)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ZonedDateTime minusSeconds() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。