I am trying to parse Instant from a CSV using OpenCsv this way:
@CsvDate("yyyy-MM-dd hh:mm:ss")
@CsvBindByName(column = "date")
private Instant date;
I know that OpenCsv is supposed to support java.time.
But when trying to use it I am getting the following exception:
Error parsing CSV line: 8.
...
Caused by: java.time.format.DateTimeParseException: Text '2022-04-21 00:00:00' could not be parsed: Unable to obtain Instant from TemporalAccessor: {HourOfAmPm=0, MicroOfSecond=0, NanoOfSecond=0, MilliOfSecond=0, MinuteOfHour=0, SecondOfMinute=0},ISO resolved to 2022-04-21 of type java.time.format.Parsed
...
Caused by: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {HourOfAmPm=0, MicroOfSecond=0, NanoOfSecond=0, MilliOfSecond=0, MinuteOfHour=0, SecondOfMinute=0},ISO resolved to 2022-04-21 of type java.time.format.Parsed
...
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
What comes from my investigation, is that the root cause is the lack of a time zone, but my question is how do I specify the time zone using the annotation only?
Instantis a point in time independent of time zone and therefore neither defines year, month, day-of-month. hour, minute nor second. So trying to parse it using a format holding those elements does not make sense, at least not if you don’t specify the time zone or offset from UTC.OpenCsv+@CsvDate. Also, I'd assume that it would get a default UTC time zone if not supplied (though it seems like it isn't).