I want some help to be able to parse out String data out of a .csv file as dynamically/flexible as possible, meaning the user can enter a bunch of different types of formats (i.e. I want to handle dd-MMM-yyyy
but also yyyy-MM-dd
and more if possible) of dates, or datetimes, and I should be able to parse without throwing exceptions or crashing. The current format for the date/datetime fields of the .csv files is dd-MMM-yyyy
so something like 30-Apr-2020
. Of course, time can be added and is optional (as seen by the pattern uses [ ] bracket notation, so that would be 30-Apr-2020 23:59:59
). I already have set up the parsing of the date/datetime columns as such:
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("dd-MMM-yyyy[[ ]['T']HH:mm:ss]")
.optionalStart()
.appendFraction(ChronoField.MICRO_OF_SECOND, 1, 6, true)
.optionalEnd()
.toFormatter();
TemporalAccessor temporalAccessor = dtf.parseBest(dateString, LocalDateTime::from, LocalDate::from);
if (temporalAccessor instanceof LocalDateTime) {
// process here
} else if (temporalAccessor instanceof LocalDate) {
// process here
}
So, basically by setting up the pattern to be flexible i.e. "dd-MMM-yyyy[[ ]['T']HH:mm:ss]"
, I then check using the TemporalAccessor whether its a date or date-time and do further processing as needed. I can process many different types of input and not have the app throw an exception here and fail. So I can consume:
01-Sep-2020 // just date
01-Sep-2099 18:59:59 // datetime
01-Apr-2033 18:59:59.123 // datetime with ms
01-Aug-2057 23:59:59.123456 // date time up to 6 ms decimal pts
However, if the user .csv contains something like 2020-05-30
date, which I believe is the ISO format standard, it will fail. Also, something bad I just noticed now, is the .parseBest()
method, also fails because its case-sensitive on the month, so something like this i.e. 01-MAY-1999
fails but 01-May-1999
passes.
How can I handle the most different types of formats without failing on parsing? As I said, I don't actually generate the .csv files (that is the Data Engineers) so I want this app to be robust/flexible as possible and be able to parse this data/correctly format it so the data can be consumed and written to the database accordingly. I thought my approach here was decent, so I was hoping a huge re-write was not needed.