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


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


Java中LocalDateTime類的withMinute()方法用於獲取此LocalDateTime的副本,並將分鍾更改為作為此方法的參數傳遞的分鍾。此LocalDateTime的其餘值保持不變。

用法:

public LocalDateTime withMinute(int minutes)

參數:此方法接受單個強製性參數minutes,該參數指定要在結果LocalDateTime實例中設置的分鍾。此分鍾的值的範圍可以從0到59。


返回值:該函數返回LocalDateTime實例,其中分鍾數更改為作為此方法的參數傳遞的分鍾數。此LocalDateTime的其餘值保持不變。

異常:如果分鍾值無效,則該函數將引發DateTimeException。

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

示例1:

// Program to illustrate the withMinute() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
        // Get the LocalDateTime instance 
        LocalDateTime dt = LocalDateTime.now(); 
  
        // Get the String representation of this LocalDateTime 
        System.out.println("Original LocalDateTime: "
                           + dt.toString()); 
  
        // Get a new LocalDateTime with minutes 0 
        System.out.println("New LocalDateTime: "
                           + dt.withMinute(0)); 
    } 
}
輸出:
Original LocalDateTime: 2018-11-30T12:52:26.105
New LocalDateTime: 2018-11-30T12:00:26.105

示例2:

// Program to illustrate the withMinute() method 
  
import java.util.*; 
import java.time.*; 
  
public class GfG { 
    public static void main(String[] args) 
    { 
        // Get the LocalDateTime instance 
        LocalDateTime dt 
            = LocalDateTime 
                  .parse("2015-04-06T10:15:30"); 
  
        // Get the String representation of this LocalDateTime 
        System.out.println("Original LocalDateTime: "
                           + dt.toString()); 
  
        // Get a new LocalDateTime with minutes 59 
        System.out.println("New LocalDateTime: "
                           + dt.withMinute(59)); 
    } 
}
輸出:
Original LocalDateTime: 2015-04-06T10:15:30
New LocalDateTime: 2015-04-06T10:59:30

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



相關用法


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