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