简体   繁体   English

使用 OpenCsv 解析 Instant

[英]Parsing Instant using OpenCsv

I am trying to parse Instant from a CSV using OpenCsv this way:我正在尝试以这种方式使用 OpenCsv 从 CSV 解析 Instant:

@CsvDate("yyyy-MM-dd hh:mm:ss")
@CsvBindByName(column = "date")
private Instant date;

I know that OpenCsv is supposed to support java.time .我知道 OpenCsv 应该支持java.time

But when trying to use it I am getting the following exception:但是在尝试使用它时出现以下异常:

Error parsing CSV line: 8.解析 CSV 行时出错: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原因:java.time.format.DateTimeParseException:无法解析文本“2022-04-21 00:00:00”:无法从 TemporalAccessor 获取 Instant:{HourOfAmPm=0,MicroOfSecond=0,NanoOfSecond=0,MilliOfSecond =0, MinuteOfHour=0, SecondOfMinute=0},ISO 解析为 2022-04-21 类型 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.DateTimeException: Unable to get Instant from TemporalAccessor: {HourOfAmPm=0, MicroOfSecond=0, NanoOfSecond=0, MilliOfSecond=0, MinuteOfHour=0, SecondOfMinute=0},ISO解析为2022-04-21类型 java.time.format.Parsed

... ...

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds引起:java.time.temporal.UnsupportedTemporalTypeException:不支持的字段: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 .请参阅DateTimeFormatter It should be HH for 24 hours time (0-23) io hh for 12 hours time (1-12). 24 小时时间 (0-23) 应为HH ,12 小时时间 (1-12) 应为 io hh The error is caused by hour "00".该错误是由小时“00”引起的。 Fortunately, otherwise you would have only the first half of the day.幸运的是,否则你将只有前半天。

See h, k, H and K.参见 h、k、H 和 K。

The class Instant does not use units like seconds, is more a long internally. class Instant不使用像秒这样的单位,在内部更长。 Use LocalDateTime for your format.使用LocalDateTime作为您的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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