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


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


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

用法:

public ZonedDateTime plusMinutes(long minutes)

參數:此方法接受一個代表分鍾數的參數分鍾,它可以是負數。


返回值:此方法基於此日期時間返回添加了分鍾數的ZonedDateTime,而不是null。

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

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

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

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

示例2:

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

ZonedDateTime before adding minutes: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
ZonedDateTime after adding minutes: 2018-10-25T23:32:31.123+02:00[Europe/Paris]

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



相關用法


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