0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javaで日時変換に詰まった件

Posted at

Javaで日時の文字列から24時間表記のyyyy/MM/dd HH:mm:ssへの変換につまったため備忘録。

NG1: Unsupported field: HourOfDay
String inputDateTime = "20230818 083000";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd hhmmss");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDate date1 = LocalDate.parse(inputDateTime, inputFormatter);
String result = date1.parse(outputFormatter);
NG2: Unsupported field: ClockHourOfAmPm
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd HHmmss");

→どちらもDateTimeFormatter.ofPatternがHHを変換できないため。

結論:LocalDateではなくLocalDateTimeを使いましょう。

OK
LocalDateTime date1 = LocalDateTime.parse(s, DateTimeFormatter.ofPattern("yyyyMMdd HHmmss"));
String result = date1.parse(outputFormatter);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?