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 给英雄的祝福语简短