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


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


ZonedDateTime類的minusMinutes()方法用於從此ZonedDateTime中減去分鍾數,並在減去後返回ZonedDateTime的副本。此方法在即時時間軸上運行,因此減去一分鍾將始終是一分鍾。此實例是不可變的,不受此方法調用的影響。

用法:

public ZonedDateTime minusMinutes(long minutes)

參數:此方法接受一個參數分鍾,表示要減去的分鍾,它可以為負。


返回值:此方法基於此日期時間返回ZonedDateTime,並減去分鍾,而不是null。

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

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

// Java program to demonstrate 
// ZonedDateTime.minusMinutes() 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 minutes: "
                           + zoneddatetime); 
  
        // subtract 15 minutes 
        ZonedDateTime returnvalue 
            = zoneddatetime.minusMinutes(15); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + " subtracting 15 minutes: "
                           + returnvalue); 
    } 
}
輸出:

ZonedDateTime before subtracting minutes: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after subtracting 15 minutes: 2018-12-06T19:06:12.123+05:30[Asia/Calcutta]

示例2:

// Java program to demonstrate 
// ZonedDateTime.minusMinutes() 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 minutes: "
                           + zoneddatetime); 
  
        // subtract 20 minutes 
        ZonedDateTime returnvalue 
            = zoneddatetime.minusMinutes(20); 
  
        // print result 
        System.out.println("ZonedDateTime after "
                           + " subtracting 20 minutes: "
                           + returnvalue); 
    } 
}
輸出:

ZonedDateTime before subtracting minutes: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
ZonedDateTime after subtracting 20 minutes: 2018-10-25T22:52:31.123+02:00[Europe/Paris]

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



相關用法


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