3
1

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 3 years have passed since last update.

javaで日時文字列を複数フォーマットでパース

Posted at

まずDateTimeFormatterを使用して複数フォーマットでパースするには、ofPatternにフォーマットを[...][...]のように指定する。

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("[yyyy-MM-dd HH:mm:ss][yyyy/MM/dd HH:mm:ss]");
    LocalDateTime p1 = LocalDateTime.parse("2020-10-27 01:01:10", formatter);
    LocalDateTime p2 = LocalDateTime.parse("2020/10/27 01:01:10", formatter);

また、Spring MVCでは@DateTimeFormatpatternに同じようにして指定する。

  @GetMapping("/sample-parse")
  public void timeformat(
      @RequestParam(name = "t1", required = false)
      @DateTimeFormat(pattern  = "[yyyy-MM-dd HH:mm:ss][yyyy/MM/dd HH:mm:ss]") LocalDateTime t1) {
    ...

参考: https://stackoverflow.com/questions/36188428/java-8-date-equivalent-to-jodas-datetimeformatterbuilder-with-multiple-parser-f

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?