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));
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP