[Java] Import the date string automatically converted to Excel with java.time.LocalDate

less than 1 minute read

When handling dates as character strings in CSV files etc., I think that the date format often uses YYYY-MM-DD. However, when you open the CSV file in Excel, it may be changed to the Excel date format and 2020-09-02 may be overwritten like 2020/9/2.

Capture with java.time.LocalDate

When importing the date format string automatically converted by Excel in this way with java.time.LocalDate, you need to specify the format with 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