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



說明

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”, 非空。

  • formatter − 要使用的格式化程式,非空。

返回值

本地日期,非空。

異常

DateTimeParseException − 如果無法解析文字。

示例

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

package com.tutorialspoint;

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
廣告