Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

java - How to trim leading space from openCsv date field?

I'm trying to parse a csv file using OpenCSV library, there is a date field that might have leading space at the beginning. I don't like to use specific formatter with leading space, because it prevents data input without leading space. Is there any workaround?
This is the Mapping Object:

public class DataObject {
  public static final String DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";
  @CsvBindByName
  private String id;
  @CsvDate(value = DATE_FORMAT)
  @CsvBindByName
  private LocalDateTime date;
}

This is the creation:

        List<DataObject> trxList = new CsvToBeanBuilder(reader)
            .withType(DataObject.class)
            .withIgnoreLeadingWhiteSpace(true)
            .build()
            .parse();

And the simplified csv file:

ID, Date
TEST, 12/05/1980 08:30:00

Caused by: java.time.format.DateTimeParseException: Text ' 20/08/2020 12:45:33' could not be parsed at index 0 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) at com.opencsv.bean.ConverterDate.lambda$determineReadTemporalConversionFunction$2(ConverterDate.java:198) at com.opencsv.bean.ConverterDate.convertToRead(ConverterDate.java:306) ... 9 more java.lang.RuntimeException: com.opencsv.exceptions.CsvDataTypeMismatchException at com.opencsv.bean.util.OpencsvUtils.handleException(OpencsvUtils.java:125) at com.opencsv.bean.concurrent.ProcessCsvLine.run(ProcessCsvLine.java:108) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

question from:https://stackoverflow.com/questions/66066568/how-to-trim-leading-space-from-opencsv-date-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...