Javaのdate formatの扱い方は?Date型をString型に変換

- SE
- Javaのdate formatはどのように扱えば良いのでしょうか。
- PM
- 日時にはformatがあり、””2020/09/09″”であれば、””yyyy/mm/dd””がformatです。このほかにもさまざまな扱い方がありますので、一緒に学んでいきましょう。
Javaのdate formatの扱い方とは?
今回は、Javaのdate formatの扱い方について説明します。日時にはformatがあります。例えば、””2020/09/09″”であれば、””yyyy/mm/dd””がformatです。
これ以外にもさまざまなformatが存在しますので、それらの扱い方について紹介します。
Javaのdate formatの扱い方に興味のある方はぜひご覧ください。
現在の日付でインスタンス生成
Javaでは、Dateクラスのインスタンスを生成して情報を取得できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 |
import java.util.Date; public class Main { public static void main(String[] args) throws Exception { // 現在の日付でインスタンス生成 Date date = new Date(); System.out.println(date); } } |
実行結果は以下のようになります。
1 |
Wed Sep 09 13:19:16 UTC 2020 |
実行時の日時で情報が表示されます。ここでは、formatは指定していないので、デフォルトのformatとなります。
このように、JavaではDateクラスのインスタンスを生成して情報を取得できます。
formatの変更
上記で紹介した以外のformatで日時を表示したい場合は、java.text.SimpleDateFormatクラスを利用します。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) throws Exception { // 現在の日付でインスタンス生成 Date date = new Date(); // formatの指定 SimpleDateFormat df1 = new SimpleDateFormat(""EEE, d MMM yyyy""); SimpleDateFormat df2 = new SimpleDateFormat(""yyyy/MM/dd'('E')' HH:mm:ss""); SimpleDateFormat df3 = new SimpleDateFormat(""hh:mm a""); System.out.println(df1.format(date)); System.out.println(df2.format(date)); System.out.println(df3.format(date)); } } |
実行結果は以下のようになります。
1 2 3 |
Wed, 9 Sep 2020 2020/09/09(Wed) 13:30:52 01:30 PM |
上記以外にも、さまざまなformatを指定できます。
以下のサイトに例が記載されています。
https://docs.oracle.com/javase/jp/8/docs/api/java/text/SimpleDateFormat.html
特定の日付でインスタンス生成
現在の日付ではなく、特定の日付でDateクラスのインスタンスを生成して情報を取得できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) throws Exception { // formatの指定 SimpleDateFormat df1 = new SimpleDateFormat(""yyyy/MM/dd""); SimpleDateFormat df2 = new SimpleDateFormat(""yyyy/MM/dd HH:mm:ss""); SimpleDateFormat df3 = new SimpleDateFormat(""hh:mm a""); // 特定の日付でインスタンス生成 Date date1 = df1.parse(""2020/10/07""); Date date2 = df2.parse(""2020/10/07 12:34:56""); Date date3 = df3.parse(""01:34 AM""); System.out.println(date1); System.out.println(date2); System.out.println(date3); } } |
実行結果は以下のようになります。
1 2 3 |
Wed Oct 07 00:00:00 UTC 2020 Wed Oct 07 12:34:56 UTC 2020 Thu Jan 01 01:34:00 UTC 1970 |
年月日を指定しないと、1970/01/01となります。
このように、Javaでは現在の日付ではなく、特定の日付でDateクラスのインスタンスを生成して情報を取得できます。
formatチェック
Javaでは、formatのチェックにDate and Time APIを利用します。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.ResolverStyle; public class Main { public static void main(String[] args) throws Exception { try{ // formatのチェック LocalDate.parse(""2020/09/31"", DateTimeFormatter.ofPattern(""uuuu/MM/dd"").withResolverStyle(ResolverStyle.STRICT)); } catch (Exception e) { System.out.println(e); } } } |
実行結果は以下のようになります。
1 |
java.time.format.DateTimeParseException: Text '2020/09/31' could not be parsed: Invalid date 'SEPTEMBER 31' |
9月に31日は存在しないので、DateTimeParseExceptionが発生します。
このように、JavaではformatのチェックにDate and Time APIを利用します。
Date型をString型に変換
Javaでは、Date型をString型に変換できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) throws Exception { // formatの指定 SimpleDateFormat df1 = new SimpleDateFormat(""yyyy/MM/dd""); SimpleDateFormat df2 = new SimpleDateFormat(""yyyy/MM/dd HH:mm:ss""); SimpleDateFormat df3 = new SimpleDateFormat(""hh:mm a""); // 特定の日付でインスタンス生成 Date date1 = df1.parse(""2020/10/07""); Date date2 = df2.parse(""2020/10/07 12:34:56""); Date date3 = df3.parse(""01:34 AM""); // Date型をString型に変換 String str1 = df1.format(date1); String str2 = df2.format(date2); String str3 = df3.format(date3); System.out.println(date1); System.out.println(date2); System.out.println(date3); System.out.println(str1); System.out.println(str2); System.out.println(str3); } } |
実行結果は以下のようになります。
1 2 3 4 5 6 |
Wed Oct 07 00:00:00 UTC 2020 Wed Oct 07 12:34:56 UTC 2020 Thu Jan 01 01:34:00 UTC 1970 2020/10/07 2020/10/07 12:34:56 01:34 AM |
このように、JavaではDate型をString型に変換できます。
- SE
- 現在の日時や特定の日時をささまざまなformatで表示やチェックができるのですね。
- PM
- Javaのdate formatの扱い方をより理解するために、ソースコードで書いてみましょう。
まとめ
いかがでしたでしょうか。Javaのdate formatの扱い方について説明しました。
現在の日時や特定の日時をささまざまなformatで表示できます。また、formatのチェックもできます。
ぜひご自身でソースコードを書いて、理解を深めてください。