0
0

More than 3 years have passed since last update.

Excelに自動で変換された日付の文字列をjava.time.LocalDateで取り込む

Last updated at Posted at 2020-09-02

CSVファイルなどで日付を文字列で扱うとき、日付のフォーマットは YYYY-MM-DD を使うことが多いかと思います。
ところが、CSVファイルをExcelで開くと勝手にExcelの日付フォーマットに変更されて 2020-09-022020/9/2 といったように上書きされてしまうことがあります。

java.time.LocalDate で取り込む

このようにExcelで自動変換された日付フォーマットの文字列を java.time.LocalDate で取り込むときは java.time.format.DateTimeFormatter でフォーマットを指定する必要があります。


scala> import java.time.LocalDate
import java.time.LocalDate

scala> import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatter

scala> val localDate = LocalDate.parse("2020/9/2", DateTimeFormatter.ofPattern("yyyy/M/d"));
localDate: java.time.LocalDate = 2020-09-02
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