简体   繁体   中英

Parsing Instant using OpenCsv

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?

See DateTimeFormatter . It should be HH for 24 hours time (0-23) io hh for 12 hours time (1-12). The error is caused by hour "00". Fortunately, otherwise you would have only the first half of the day.

See h, k, H and K.

The class Instant does not use units like seconds, is more a long internally. Use LocalDateTime for your format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM