I have the following field in an entity:
private LocalDate birthDate;
I try to get date fields from CSV file using Apache Commons CSV and need to convert the read date field as LocalDate
with the format I set in formatter:
// the read dates in the csv file are like that: 5/14/1974
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
// I also tried using the same format
// private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
// code omittted for brevity
LocalDate.parse(csvRecord.get(Headers.BirthDate), formatter); // gives error
csvRecord.get(Headers.BirthDate)
gives date correctly as 4/6/1986
in string format, but LocalDate.parse(...)
cannot parse and gives "Text '4/6/1986' could not be parsed at index 0" error. So, what is wrong in my implementation? If I change date in csv file to 1986/6/1
then LocalDate.parse()
working. Maybe I need to read the data in this format and then convert to desired format.
I can use some approach like that, but I need to get date as LocalDate
format instead of String
and that may be the factor causing error:
public static void main(String[] args) {
Locale.setDefault(Locale.FRANCE);
// no problem now, DateTimeFormatter always uses Locale.US
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.US);
String date = "16-Aug-2016";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate); //default, print ISO_LOCAL_DATE
System.out.println(formatter.format(localDate)); // print formatted date
}