CSVファイルなどで日付を文字列で扱うとき、日付のフォーマットは YYYY-MM-DD
を使うことが多いかと思います。
ところが、CSVファイルをExcelで開くと勝手にExcelの日付フォーマットに変更されて 2020-09-02
が 2020/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