java.time.MonthDay.format(DateTimeFormatter formatter)方法

java.time.MonthDay.format(DateTimeFormatter formatter)方法使用指定的格式化程序格式化此月-日

声明

以下是java.time.MonthDay.format(DateTimeFormatter formatter)方法的声明。

public String format(DateTimeFormatter formatter)

参数

  • formatter - 要使用的格式化程序,不能为null

返回值

格式化的日期字符串,不能为null

异常

  • DateTimeException - 如果在打印期间发生错误。

示例

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

package com.zaixian;

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

上一篇: java.time.MonthDay类 下一篇: java.time.OffsetDateTime类