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



說明

java.time.LocalDate.parse(CharSequence text, DateTimeFormatter formatter) 方法使用特定格式程式,從文字字串獲取 LocalDate 的一個例項。

宣告

以下是 java.time.LocalDate.parse(CharSequence text, DateTimeFormatter formatter) 方法的宣告。

public static LocalDate parse(CharSequence text, DateTimeFormatter formatter)

引數

  • text − 要解析的文字,例如“2017-02-03”,不能為空。

  • formatter − 要使用的格式程式,不能為空。

返回值

本地日期,不能為空。

異常

DateTimeParseException − 如果該文字無法解析。

示例

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

package com.tutorialspoint;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateDemo {
   public static void main(String[] args) {
	   
	   DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM uuuu");
	   String date = "03 Feb 2017";
	   LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
	   System.out.println(localDate);
   }
}

讓我們編譯並執行以上程式,它將生成以下結果 −

2017-02-03
廣告
© . All rights reserved.