JavaでのDateとStringの変換ガイド
Javaで日付と文字列を変換する方法は多数あります。
この記事では、日付を文字列に変換し、文字列を日付に変換する方法を詳しく解説します。
1. SimpleDateFormatを使用してDateをStringに変換
JavaにはSimpleDateFormat
というクラスがあり、これを使用することでDateをStringに簡単に変換することができます。
ソースコード例
1 2 3 4 5 6 7 8 9 10 11 |
import java.text.SimpleDateFormat; import java.util.Date; public class Program { public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(now); System.out.println(formattedDate); } } |
出力結果
2023-08-17 14:32:15

KindleUnlimited会員であれば、全ての本をご覧頂けます。 StreamAPIを理解すれば、Javaの世界が変わる 第1版

2. StringをDateに変換
逆に、文字列をDateに変換する方法もSimpleDateFormat
を使用して行うことができます。
ソースコード例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Program { public static void main(String[] args) { String dateStr = "2023-08-17 14:32:15"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date parsedDate = sdf.parse(dateStr); System.out.println(parsedDate); } catch (ParseException e) { e.printStackTrace(); } } } |
出力結果
Thu Aug 17 14:32:15 JST 2023

3. LocalDateTimeとDateTimeFormatterを利用した変換
Java 8からは、LocalDateTime
とDateTimeFormatter
を利用して日付と文字列の変換が行えます。
これを利用することでより簡単かつ柔軟に日付の変換が行えます。
ソースコード例
1 2 3 4 5 6 7 8 9 10 11 |
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Program { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDate = now.format(formatter); System.out.println(formattedDate); } } |
出力結果
2023-08-17 14:32:15

4. ZoneDateTimeを利用したタイムゾーン考慮の変換
タイムゾーンを考慮した日付の取り扱いにはZonedDateTime
を利用することが推奨されます。
これにより、異なるタイムゾーンでの日付の変換も容易に行えます。
ソースコード例
1 2 3 4 5 6 7 8 9 10 11 |
import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Program { public static void main(String[] args) { ZonedDateTime now = ZonedDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss ZZZZ"); String formattedDate = now.format(formatter); System.out.println(formattedDate); } } |
出力結果
2023-08-17 14:32:15 +0900 Asia/Tokyo

5. Stringの日時フォーマット変更
String型の日時を別のフォーマットのString型に変換したい場合の方法です。
これは日時を表示する際のフォーマットを変更したい時に役立ちます。
ソースコード例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Program { public static void main(String[] args) { String strDate = "2023-08-17 12:34:56"; SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdfDest = new SimpleDateFormat("yyyy/MM/dd HH:mm"); try { Date date = sdfSource.parse(strDate); String formattedDate = sdfDest.format(date); System.out.println(formattedDate); } catch (ParseException e) { e.printStackTrace(); } } } |
出力結果 2023/08/17 12:34

まとめ
この記事では、JavaでのDate型とString型の日時の相互変換方法について、ソースコード例を交えて解説しました。
これをマスターすることで、日時の扱いがよりスムーズになります。
最後まで読んで頂き、ありがとうございました。少しでもお役にたてたなら幸いです!
