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


Java OffsetDateTime withMinute()用法及代碼示例


Java中OffsetDateTime類的withMinute()方法返回此OffsetDateTime的副本,其中一天中的小時數按照參數中的指定進行了更改。

用法:

public OffsetDateTime withMinute(int minute)

參數:此方法接受單個參數分鍾,該分鍾指定要在結果中設置的minute-of-hour,範圍為0到59。


返回值:它基於此日期返回OffsetDateTime,其中包含請求的小時數,而不是null。

異常:當小時的分鍾值無效或day-of-year無效時,程序將引發DateTimeException。

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

示例1:

// Java program to demonstrate the withMinute() method 
  
import java.time.OffsetDateTime; 
import java.time.ZonedDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Parses the date1 
        OffsetDateTime date1 
            = OffsetDateTime 
                  .parse( 
                      "2018-12-12T13:30:30+05:00"); 
  
        // Prints dates 
        System.out.println("Date1: " + date1); 
  
        // Changes the minute of day 
        System.out.println("Date1 after altering minute of day: "
                           + date1.withMinute(20)); 
    } 
}
輸出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after altering minute of day: 2018-12-12T13:20:30+05:00

示例2:

// Java program to demonstrate the withMinute() method 
  
import java.time.OffsetDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
            // Parses the date1 
            OffsetDateTime date1 
                = OffsetDateTime 
                      .parse( 
                          "2018-12-12T13:30:30+05:00"); 
  
            // Prints dates 
            System.out.println("Date1: " + date1); 
  
            // Changes the minute of day 
            System.out.println("Date1 after altering minute of day: "
                               + date1.withMinute(27)); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Date1: 2018-12-12T13:30:30+05:00
Date1 after altering minute of day: 2018-12-12T13:27:30+05:00

參考: https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#withMinute(int)



相關用法


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