当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。