Java 中的 MonthDay withDayOfMonth() 方法


使用 Java 中 MonthDay 類的 withDayOfMonth() 方法可以建立一個 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.withDayOfMonth(05);
      System.out.println("The MonthDay with day of month altered is: " + md2);
   }
}

輸出

The MonthDay is: --02-22
The MonthDay with day of month altered is: --02-05

現在讓我們瞭解一下上述程式。

首先,顯示 MonthDay。然後,使用 withDayOfMonth() 方法顯示 MonthDay,其中的天已更改為 05。下面是演示此操作的程式碼片段 -

MonthDay md1 = MonthDay.parse("--02-22");
System.out.println("The MonthDay is: " + md1);
MonthDay md2 = md1.withDayOfMonth(05);
System.out.println("The MonthDay with day of month altered is: " + md2);

更新於: 2019 年 7 月 30 日

73 次瀏覽

開啟您的 事業

完成課程,獲得認證

開始
廣告
© . All rights reserved.