Java中的LocalDateTime withYear()方法
使用withYear()Java中LocalDateTime类中的方法,可以根据需要更改年份的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.withYear(2020);
System.out.println("The LocalDateTime with year altered is: " + ldt2);
}
}输出结果
The LocalDateTime is: 2019-02-18T23:15:30 The LocalDateTime with year altered is: 2020-02-18T23:15:30
现在让我们了解上面的程序。
首先显示LocalDateTime。然后使用方法显示年份更改为2020的LocalDateTimewithYear()。演示这的代码片段如下-
LocalDateTime ldt1 = LocalDateTime.parse("2019-02-18T23:15:30");
System.out.println("The LocalDateTime is: " + ldt1);
LocalDateTime ldt2 = ldt1.withYear(2020);
System.out.println("The LocalDateTime with year altered is: " + ldt2);热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短