Java中的LocalDate withMonth()方法
使用Java中LocalDate类中的withMonth()方法,可以完成按需更改月份的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.withMonth(05);
System.out.println("The LocalDate with month altered is: " + ld2);
}
}输出结果
The LocalDate is: 2019-02-15 The LocalDate with month altered is: 2015-05-15
现在让我们了解上面的程序。
首先显示LocalDate。然后使用withMonth()方法显示月份更改为05的LocalDate。演示这的代码片段如下-
LocalDate ld1 = LocalDate.parse("2019-02-15");
System.out.println("The LocalDate is: " + ld1);
LocalDate ld2 = ld1.withMonth(05);
System.out.println("The LocalDate with month altered is: " + ld2);