Java中的 LocalTime withMinute() 方法及示例
LocalTime类的withMinute()方法用于获取此 LocalTime 的副本,其中分钟更改为作为参数传递给此方法的分钟。此 LocalTime 的其余值将保持不变。此实例是不可变的,不受此方法调用的影响。
句法:
public LocalTime withMinute(int minute)
参数:此方法接受单个参数minute ,它表示要在结果中设置的分钟,从 0 到 59。
返回值:该方法根据这个时间和请求的分钟返回一个LocalTime 实例。
异常:如果分钟值无效,此方法会抛出异常DateTimeException
下面的程序说明了 withMinute() 方法:
方案一:
// 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
方案二:
// 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: Java/time/LocalTime.html#withMinute(int)