Java 中的 MonthDay with() 方法
在 Java 中 MonthDay 类的 with() 方法用于创建一个不可变的 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。然后使用 withMonth() 方法显示将月份年更改为 AUGUST 的 MonthDay。演示此功能的代码片段如下 -
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);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP