Javatime-localdatetime-parse1

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

java.time.LocalDateTime.parse()メソッドの例

説明

  • java.time.LocalDateTime.parse(CharSequence text、DateTimeFormatter formatter)*メソッドは、特定のフォーマッターを使用してテキスト文字列からLocalDateTimeのインスタンスを取得します。

宣言

以下は、* java.time.LocalDateTime.parse(CharSequence text、DateTimeFormatter formatter)*メソッドの宣言です。

public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)

パラメーター

  • text -「2017-02-03T10:15:30」などの解析するテキスト、null以外。
  • formatter -使用するフォーマッタ、nullではない。

戻り値

ローカル日付、null以外。

例外

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

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

package com.finddevguides;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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

      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
      String date = "2017-12-03T10:15:30+01:00";
      LocalDateTime date1 = LocalDateTime.parse(date, dateTimeFormatter);
      System.out.println(date1);
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

2017-12-03T10:15:30