Java中的LocalDateTime withMonth()方法
使用Java中LocalDateTime类中的withMonth()方法,可以完成按需更改月份的LocalDateTime的不可变副本。此方法需要一个参数,即要在LocalDateTime中设置的月份,并且它返回LocalDateTime,并根据需要更改月份。
演示此的程序如下所示-
示例
import java.time.*;
public class Main {
public static void main(String[] args) {
LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30");
System.out.println("The LocalDateTime is: " + ldt1);
LocalDateTime ldt2 = ldt1.withMonth(7);
System.out.println("The LocalDateTime with month altered is: " + ldt2);
}
}输出结果
The LocalDateTime is: 2019-02-18T23:15:30 The LocalDateTime with month altered is: 2019-07-18T23:15:30
现在让我们了解上面的程序。
首先显示LocalDateTime。然后使用withMonth()方法显示月份更改为7的LocalDateTime。演示这的代码片段如下-
LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30");
System.out.println("The LocalDateTime is: " + ldt1);
LocalDateTime ldt2 = ldt1.withMonth(7);
System.out.println("The LocalDateTime with month altered is: " + ldt2);热门推荐
1 简短元宵节祝福语
10 新年顾客简短祝福语大全
11 搞笑英译祝福语简短句
12 生日祝福语男友妈妈简短
13 客户祝福语事业文案简短
14 生气蛋糕祝福语大全简短
15 商务感恩祝福语大全简短
16 生日祝福语简短女友英语
17 白族结婚祝福语简短大全
18 牛年祝福语简短走心