Javatime-localdatetime-format
提供:Dev Guides
java.time.LocalDateTime.format()メソッドの例
説明
- java.time.LocalDateTime.format(DateTimeFormatter formatter)*メソッドは、指定されたフォーマッターを使用してこの日時をフォーマットします。
宣言
以下は* java.time.LocalDateTime.format(DateTimeFormatter formatter)*メソッドの宣言です。
public String format(DateTimeFormatter formatter)
パラメーター
*formatter* -使用するフォーマッタ、nullではない。
戻り値
書式設定された日付文字列、null以外
例外
*DateTimeException* -印刷中にエラーが発生した場合。
例
次の例は、java.time.LocalDateTime.format(DateTimeFormatter formatter)メソッドの使用方法を示しています。
package com.finddevguides;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeDemo {
public static void main(String[] args) {
LocalDateTime date = LocalDateTime.parse("2017-02-03T12:30:30");
System.out.println(date);
DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME;
System.out.println(formatter.format(date));
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
2017-02-03T12:30:30
12:30:30