MonthDay 格式化()方法在 Java 中


MonthDay 可以在 Java 中 MonthDay 類的 format() 方法使用特定的格式化器進行格式化。此方法需要一個引數,即要格式化的 MonthDay 物件,它返回使用特定格式化器格式化的 MonthDay。

以下給出了演示此方法的程式

示例

 線上演示

import java.time.*;
import java.time.temporal.*;
import java.time.format.DateTimeFormatter;
   public class Demo {
      public static void main(String[] args) {
      MonthDay md = MonthDay.parse("--02-22");
      LocalDate ld = md.atYear(2019);
      System.out.println("The MonthDay is: " + md);
      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
      System.out.println("The formatted MonthDay is: " + dtf.format(ld));
   }
}

輸出

The MonthDay is: --02-22
The formatted MonthDay is: 2019-02-22

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

首先顯示 MonthDay。然後使用 format() 方法使用特定的格式化器對 MonthDay 進行格式化,並顯示格式化的 MonthDay。以下程式碼片段演示了此操作

MonthDay md = MonthDay.parse("--02-22");
LocalDate ld = md.atYear(2019);
System.out.println("The MonthDay is: " + md);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
System.out.println("The formatted MonthDay is: " + dtf.format(ld));

更新日期:2019-07-30

75 次瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.