Javatime-localdate-parse1

提供:Dev Guides
移動先:案内検索

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」などの解析するテキスト、nullではない。
  • formatter -使用するフォーマッタ、nullではない。

戻り値

ローカル日付、null以外。

例外

*DateTimeParseException* -テキストを解析できない場合。

次の例は、java.time.LocalDate.parse(CharSequence text、DateTimeFormatter formatter)メソッドの使用方法を示しています。

package com.finddevguides;

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