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 − 要使用的格式化程序,而不是 null。


返回值

本地日期,不为空。


异常

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