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


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


LocalTime類的withMinute()方法用於獲取此LocalTime的副本,並將分鍾更改為作為此方法的參數傳遞的分鍾。此LocalTime的其餘值將保持不變。此實例是不可變的,不受此方法調用的影響。

用法:

public LocalTime withMinute(int minute)

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


返回值:此方法基於此時間和請求的分鍾返回LocalTime實例。

異常:如果分鍾值無效,則此方法引發異常DateTimeException

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

示例1:

// Java program to demonstrate 
// LocalTime.withMinute() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime object 
        LocalTime time 
            = LocalTime.parse("19:34:50.63"); 
  
        // print time 
        System.out.println("Old LocalTime: "
                           + time); 
  
        // Get a new LocalDateTime with minutes 4 
        LocalTime newtime = time.withMinute(4); 
  
        // print result 
        System.out.println("New LocalDateTime: "
                           + newtime); 
    } 
}
輸出:
Old LocalTime: 19:34:50.630
New LocalDateTime: 19:04:50.630

示例2:

// Java program to demonstrate 
// LocalTime.withMinute() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a LocalTime object 
        LocalTime time 
            = LocalTime.parse("01:21:30.13"); 
  
        // print time 
        System.out.println("Old LocalTime: "
                           + time); 
  
        // Get a new LocalDateTime with minutes 23 
        LocalTime newtime = time.withMinute(23); 
  
        // print result 
        System.out.println("New LocalDateTime: "
                           + newtime); 
    } 
}
輸出:
Old LocalTime: 01:21:30.130
New LocalDateTime: 01:23:30.130

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



相關用法


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