Java

【これでOK】Java日時の型変換(20パターン解説)

Java日時の型変換(文字列, 日時, localdate, date, string)

今回は、Javaの日時変換について、まとめます。

このページだけ見れば、Javaのすべての日時変換ができるようになります。

↓本記事で解説(コピペでOK)

Javaの日時変換まとめ

  1. Date → Datetime
  2. Date → LocalDate
  3. Date → LocalDatetime
  4. Date → String

  5. Datetime → Date
  6. Datetime → LocalDate
  7. Datetime → LocalDatetime
  8. Datetime → String

  9. LocalDate → LocalDatetime
  10. LocalDate → Date
  11. LocalDate → Datetime
  12. LocalDate → String

  13. LocalDatetime → LocalDate
  14. LocalDatetime → Date
  15. LocalDatetime → Datetime
  16. LocalDatetime → String

  17. String → Date
  18. String → Datetime
  19. String → LocalDate
  20. String → LocalDatetime

すべてコピペでOK!

筆者

Javaの日時変換は、覚えなくてOK

→ まとめページさえあれば解決

Dateから日時変換(Java)

(1) Date to Datetime 変換(Java)

// Date → Datetime の変換
Date date = new Date();
DateTime dateTime = new DateTime(date);

(2) Date to LocalDate 変換(Java)

// Date → Localdate の変換
Date date = new Date();
LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());

(3) Date to LocalDatetime 変換(Java)

// Date → Localdatetime の変換
Date date = new Date();
LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

(4) Date to String 変換(Java)

// Date → String(yyyy-MM-dd) の変換 ... どれでもOK
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.format(date);

// Date → String(yyyy/MM/dd) の変換 ... どれでもOK
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
dateFormat.format(date);

// Date → String(yyyy/MM/dd hh:mm:ss) の変換 ... どれでもOK
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
dateFormat.format(date);

Datetime から変換(Java)

(5) Datetime to Date 変換(Java)

// Datetime → Date
DateTime dateTime = new DateTime();
dateTime.toDate();

(6) Datetime to LocalDate 変換(Java)

// Datetime → LocalDate
DateTime dateTime = new DateTime();
LocalDate localDate = LocalDate.ofInstant(dateTime.toDate().toInstant(), ZoneId.systemDefault());

DatetimeからLocalDateへの変換の流れ

  1. DatetimeをDateへ変換
  2. DateからLocalDateへ変換(完了)

(7) Datetime to LocalDatetime 変換(Java)

// Datetime → LocalDatetime
DateTime dateTime = new DateTime();
LocalDateTime localDateTime = LocalDateTime.ofInstant(dateTime.toDate().toInstant(), ZoneId.systemDefault());

DatetimeからLocalDatetimeへの変換の流れ

  1. DatetimeをDateへ変換
  2. DateからLocalDatetimeへ変換(完了)

(8) Datetime to String 変換(Java)

// Datetime → String
DateTime dateTime = new DateTime();
String dateTimeString1 = dateTime.toString("yyyy-MM-dd"); // 2020-01-01
String dateTimeString2 = dateTime.toString("yyyy/MM/dd"); // 2020/01/01
String dateTimeString3 = dateTime.toString("yyyy/MM/dd hh:mm:ss"); // 2020/01/01 12:00:00

LocalDateからの変換(Java)

(9) LocalDate to LocalDatetime 変換(Java)

// LocalDate → LocalDatetime
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = localDate.atStartOfDay();

(10) LocalDate to Date 変換(Java)

// LocalDate → Date
LocalDate localDate = LocalDate.now();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(localDate.toString());

(11) LocalDate to Datetime 変換(Java)

// LocalDate → Datetime
LocalDate localDate = LocalDate.now();
DateTime dateTime = new DateTime(localDate);

(12) LocalDate to String 変換(Java)

// LocalDate → String
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String localDateString = localDate.format(dateTimeFormatter);

LocalDatetime から変換(Java)

(13) LocalDatetime to LocalDate 変換(Java)

// LocalDatetime to LocalDate
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();

(14) LocalDatetime to Date 変換(Java)

// LocalDatetime to Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()).toInstant());

(15) LocalDatetime to Datetime 変換(Java)

// LocalDatetime to Datetime
LocalDateTime localDateTime = LocalDateTime.now();
DateTime dateTime = new DateTime(localDateTime);

(16) LocalDatetime to String 変換(Java)

// LocalDatetime to String
LocalDateTime localDateTime = LocalDateTime.now();
String string = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

String から変換(Java)

(17) String to Date 変換(Java)

// String → Date
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date date = format.parse("2021/01/01");

(18) String to Datetime 変換(Java)

// String → Datetime
String string = "2021/01/01";
DateTime dateTime = DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime(string);

(19) String to LocalDate 変換(Java)

// String → LocalDate
String string = "2021/01/01";
LocalDate localDate = LocalDate.parse(string, DateTimeFormatter.ofPattern("yyyy/MM/dd"));

(20) String to LocalDatetime 変換(Java)

// String → LocalDate
String string = "2021/01/01";
LocalDateTime localDateTime = LocalDateTime.parse(string, DateTimeFormatter.ofPattern("yyyy/MM/dd"));