How do I parse a date from CSV ("Wednesday, May 5, 2021") into LocalDate in Kotlin? @CsvDate(value = "E, MMMM d, yyyy")
isn't working for me.
@Entity
@Data
@Getter
@Setter
@ToString
@Table(name="used_vacation")
data class UsedVacation(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id:Long,
@Column(name="date_start") @CsvDate(value = "E, MMMM d, yyyy") @CsvBindByPosition(position = 1) val dateStart:LocalDate,
@Column(name="date_end") @CsvDate(value = "E, MMMM d, yyyy") @CsvBindByPosition(position = 2) val dateEnd:LocalDate,
@ManyToOne @JoinColumn(name="employee_id",nullable=false) @CsvBindByPosition(position = 0) val employee:Employee,
)
{
private fun createCSVToBeanUsedVacation(fileReader: BufferedReader?): CsvToBean<UsedVacation> =
CsvToBeanBuilder<UsedVacation>(fileReader)
.withType(UsedVacation::class.java)
.withSkipLines(2)
.withIgnoreLeadingWhiteSpace(true)
.build()
I get this error:
Caused by: java.time.format.DateTimeParseException: Text 'Thursday, October 24, 2019' could not be parsed at index 3
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at com.opencsv.bean.ConverterDate.lambda$determineReadTemporalConversionFunction$9(ConverterDate.java:216)
at com.opencsv.bean.ConverterDate.convertToRead(ConverterDate.java:306)
... 9 more
CodePudding user response:
Caused by: java.time.format.DateTimeParseException: Text 'Thursday, October 24, 2019' could not be parsed at index 3
This happens because of the E, MMMM d, yyyy
date pattern you are currently using: the E
char for days implies that a short or abbreviated form for days will be used if available so the date parser expects to meet an abbreviated form for Thursday like Thu
and then the error. To parse the Thursday day you can instead use the four E
characters in the EEEE, MMMM d, yyyy
pattern because if the number of pattern letters is 4 or more it informs the date parser that the full form for days will be used.