Java中的LocalDate withYear()方法
使用withYear()Java中LocalDate类中的方法,可以完成按需更改年份的LocalDate的不可变副本。此方法需要一个参数,即要在LocalDate中设置的年份,并且它返回LocalDate并根据需要更改年份。
演示此的程序如下所示-
示例
import java.time.*;
public class Main {
public static void main(String[] args) {
LocalDate ld1 = LocalDate.parse("2019-02-15");
System.out.println("The LocalDate is: " + ld1);
LocalDate ld2 = ld1.withYear(2015);
System.out.println("The LocalDate with year altered is: " + ld2);
}
}输出结果
The LocalDate is: 2019-02-15 The LocalDate with year altered is: 2015-02-15
现在让我们了解上面的程序。
首先显示LocalDate。然后使用方法显示年份更改为2015的LocalDatewithYear()。演示这的代码片段如下-
LocalDate ld1 = LocalDate.parse("2019-02-15");
System.out.println("The LocalDate is: " + ld1);
LocalDate ld2 = ld1.withYear(2015);
System.out.println("The LocalDate with year altered is: " + ld2);热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便