JavaにおけるDateTimeFormatterとは?
JavaのDateTimeFormatterとは、java.timeパッケージに含まれるクラスです。Java 8から導入されたものであり、日付や時刻を指定した形式でフォーマットするための機能を提供しています。
DateTimeFormatterは、従来のSimpleDateFormatクラスと比べると、不変(immutable)でスレッドセーフな設計です。スレッドセーフとは、マルチスレッド環境であるプログラムを複数スレッドで並行的に実行しても、問題が起こらない安全な仕様を意味します。
DateTimeFormatterクラスを使うと、LocalDate、LocalDateTime、ZonedDateTimeなどの日時オブジェクトを読みやすい文字列形式に変換したり、逆に文字列から日時オブジェクトを生成したりすることが可能です。現代のJava開発においては、日付処理を行う際の標準的なツールとして広く活用されています。
【関連】
Javaをもっと詳しく学ぶならpaizaラーニング
基本構文
JavaのDateTimeFormatterにおける基本的な使用方法は、まずフォーマッターを作成し、それを使って日時オブジェクトをフォーマットするという流れです。フォーマッターの作成には、事前定義されたパターンを使用することのほかに、独自パターンを指定する方法があります。代表的な使用例を以下で示しましょう。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter1 = DateTimeFormatter.ISO_LOCAL_DATE;
LocalDate date1 = LocalDate.now();
System.out.println(formatter1.format(date1));
}
}
出力例:
2024-01-15
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
LocalDate date2 = LocalDate.of(2024, 3, 21);
System.out.println(formatter2.format(date2));
}
}
出力結果:
2024年03月21日
実用例
このセクションでは、JavaのDateTimeFormatterの実践的な使用例を8つのパターンに分けて詳しく解説します。
各コード例では、実際の開発現場でよく使われるフォーマットパターンを取り上げ、具体的なコード例とその出力結果を示しましょう。
DateTimeFormatterの理解を深め、実際のアプリケーション開発などで活用していくためには、このセクションで示されたコードを自分の環境で書いてみる経験が必要です。さまざまなコードに触れて動かしてみると、Javaプログラミングに不可欠な課題解決力や判断力なども身についてくるでしょう。
失敗を恐れず、多くのコードに触れてみてください
基本的な日付フォーマット
以下で示しているのは、日付フォーマットのなかでも最もシンプルな例です。年月日を日本語形式で表示するパターンとなっており、DateTimeFormatter.ofPattern()メソッドを使ってカスタムフォーマッターを作成しています。この方法はユーザーインターフェースでの日付表示などによく使われるものでしょう。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate dogDate = LocalDate.of(2024, 4, 15);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println("イヌの誕生日: " + formatter.format(dogDate));
}
}
出力結果:
イヌの誕生日: 2024年04月15日
時刻を含む完全なフォーマット
以下で示しているのは、日付と時刻の両方を含む完全な日時情報をフォーマットする方法です。LocalDateTimeクラスを使い、年月日に加えて時分秒まで表示しています。ログファイルやタイムスタンプの記録をする際に最適なフォーマットパターンです。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime catTime = LocalDateTime.of(2024, 6, 20, 14, 30, 45);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println("ネコの診察時間: " + formatter.format(catTime));
}
}
出力結果:
ネコの診察時間: 2024/06/20 14:30:45
カスタムパターンでの日付表示
以下で示しているのは、独自のパターンを使い、より読みやすい形式で日付を表示する例です。曜日を含めたカジュアルな表示形式となっており、アプリケーションのユーザーインターフェースやお知らせ表示などに役立つフォーマットになります。Eパターンを使い、曜日もあわせて表示したところもポイントでしょう。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDate rabbitDate = LocalDate.of(2024, 12, 8);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月d日(E)", Locale.JAPANESE);
System.out.println("ウサギの健康診断: " + formatter.format(rabbitDate));
}
}
出力結果:
ウサギの健康診断: 12月8日(日)
和暦での日付表示
以下で示しているのは、日本の和暦を使用した日付フォーマットの実装例です。JapaneseDateクラスを活用して西暦を和暦に変換し、令和などの元号を含めた日本独特の日付表示を実現しています。公的文書や日本国内向けアプリケーションでよく使用されるパターンでしょう。
import java.time.LocalDate;
import java.time.chrono.JapaneseDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDate hamsterDate = LocalDate.of(2024, 3, 10);
JapaneseDate japaneseDate = JapaneseDate.from(hamsterDate);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Gy年M月d日", Locale.JAPAN);
System.out.println("ハムスターの記録: " + formatter.format(japaneseDate));
}
}
出力結果:
ハムスターの記録: 令和6年3月10日
12時間表記での時刻表示
以下で示しているのは、午前午後を含む12時間表記での時刻フォーマット例です。24時間表記ではなく、一般的に馴染みのある午前午後形式で時刻を表示しています。aパターンで午前午後、hパターンで12時間制の時刻を表現した、ユーザーフレンドリーな時刻表示の方法です。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDateTime birdTime = LocalDateTime.of(2024, 5, 18, 15, 45);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日 a h:mm", Locale.JAPAN);
System.out.println("トリのえさ時間: " + formatter.format(birdTime));
}
}
出力結果:
トリのえさ時間: 2024年5月18日 午後 3:45
曜日を含む詳細な日付表示
以下で示しているのは、曜日情報を含めた詳細な日付フォーマットの実装方法です。EEEEパターンを使うことで曜日の完全形(月曜日、火曜日など)を表示できます。スケジュール管理アプリケーションの開発やカレンダー表示をする際に、特に役立つフォーマットパターンです。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDate fishDate = LocalDate.of(2024, 8, 25);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 EEEE", Locale.JAPANESE);
System.out.println("サカナの水替え: " + formatter.format(fishDate));
}
}
出力結果:
サカナの水替え: 2024年08月25日 日曜日
タイムゾーン付きの日時表示
以下で示しているのは、タイムゾーン情報を含む日時フォーマットの使用例です。ZonedDateTimeクラスを使って特定のタイムゾーンでの日時を表現し、zパターンでタイムゾーン略称を表示します。国際的なアプリケーションや複数タイムゾーンを扱うシステムで特に重要なフォーマットです。
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
ZonedDateTime lionTime = ZonedDateTime.of(2024, 7, 12, 16, 20, 0, 0, ZoneId.of("Asia/Tokyo"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm z");
System.out.println("ライオンの見学時間: " + formatter.format(lionTime));
}
}
出力結果:
ライオンの見学時間: 2024/07/12 16:20 JST
数値のみでの日付表示
以下で示しているのは、区切り文字を使わない数値のみでの日付フォーマット例です。yyyyMMddパターンを使って8桁の数値形式で日付を表現します。データベースのキーやファイル名、バッチ処理の識別子といったシステム内部での日付識別で頻繁に使用される実用的なフォーマットです。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate elephantDate = LocalDate.of(2024, 11, 3);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
System.out.println("ゾウの管理番号: " + formatter.format(elephantDate));
}
}
出力結果:
ゾウの管理番号: 20241103
まとめ
JavaのDateTimeFormatterは、現代のJava開発において日付フォーマット処理を行う際の標準ツールです。従来のSimpleDateFormatと比べてスレッドセーフで使いやすく、豊富なフォーマットパターンに対応しています。
Javaプログラミング初心者の皆さんは、少しずつステップアップしながらDateTimeFormatterの活用方法を身につけてみてください。DateTimeFormatterをマスターすると、さまざまな開発現場での日付処理要件に対して柔軟な対応ができるようになるでしょう。
レベルを更に上げたい方はpaizaプログラミングスキルチェック