java.time.MonthDay.parse(CharSequence text, DateTimeFormatter formatter)方法

java.time.MonthDay.parse(CharSequence text,DateTimeFormatter formatter)方法使用特定格式化程式從文本字串中獲取MonthDay的實例。

聲明

以下是java.time.MonthDay.parse(CharSequence text,DateTimeFormatter formatter)方法的聲明。

public static MonthDay parse(CharSequence text, DateTimeFormatter formatter)

參數

  • text - 要解析的文本,例如:"--10-15",而不是null
  • formatter - 要使用的格式化程式,而不是null

返回值

本地日期,不為null

異常

  • DateTimeParseException - 如果無法解析文本。

示例

以下示例顯示了java.time.MonthDay.parse(CharSequence text,DateTimeFormatter formatter)方法的用法。

package com.zaixian;

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

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

      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("--MM-dd");
      String date = "--10-15";
      MonthDay date1 = MonthDay.parse(date, dateTimeFormatter);
      System.out.println(date1);
   }
}

編譯並運行上面的程式,這將產生以下結果 -

--10-15

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