java.time.MonthDay.format() 方法示例



說明

**java.time.MonthDay.format(DateTimeFormatter formatter)** 方法使用指定的格式程式格式化此天數。

宣告

以下是 **java.time.MonthDay.format(DateTimeFormatter formatter)** 方法的宣告。

public String format(DateTimeFormatter formatter)

引數

formatter - 要使用的格式程式,非空。

返回值

格式化的日期字串,非空。

異常

DateTimeException - 如果列印期間發生錯誤。

示例

以下示例演示了 java.time.MonthDay.format(DateTimeFormatter formatter) 方法的使用。

package com.tutorialspoint;

import java.time.MonthDay;
import java.time.format.DateTimeFormatter;

public class MonthDayDemo {
   public static void main(String[] args) {

      MonthDay date = MonthDay.parse("--12-30");
      System.out.println(date);  
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("--MM-dd");
      System.out.println(formatter.format(date));  
   }
}

讓我們編譯並執行以上程式,結果如下所示 −

--12-30
--12-30
廣告