使用with()Java中的MonthDay类中的方法来完成MonthDay的不可变副本,并根据需要更改一年中的月份。此方法需要一个参数,即要在MonthDay中设置的月份,并且它返回MonthDay,并根据需要更改年份中的月份。
演示此的程序如下所示-
import java.time.*;
public class Demo {
public static void main(String[] args) {
MonthDay md1 = MonthDay.parse("--02-22");
System.out.println("The MonthDay is: " + md1);
MonthDay md2 = md1.with(Month.AUGUST);
System.out.println("The MonthDay with month of year altered is: " + md2);
}
}输出结果
The MonthDay is: --02-22 The MonthDay with month of year altered is: --08-22
现在让我们了解上面的程序。
首先显示MonthDay。然后使用方法显示将月份中的月份更改为AUGUST的MonthDay withMonth()。演示这的代码片段如下-
MonthDay md1 = MonthDay.parse("--02-22");
System.out.println("The MonthDay is: " + md1);
MonthDay md2 = md1.with(Month.AUGUST);
System.out.println("The MonthDay with month of year altered is: " + md2);