1

Im using univocity for parsing csv file. But I cant parse date in String format to java.util.Date. It seems like the @Format(formats ="YYYY-MM-DD") annotation is not working.

Im trying parse line

2015-04-30,67,"P",1972-02-28,2006-08-11,"97912a4321dd510","49d180ecf56132819571bf39d9b7b34"

but it gives me this format after parsing, which is not YYYY-MM-DD. Can you help me?

day=Sun Dec 28 00:00:00 CET 2014, id=5151, type=B, originDate=Sun Dec 31 00:00:00 CET 1950, relDate=Sun Dec 26 00:00:00 CET 2010, legalId=3bbfca2849c01ad, name=236e5fcfd21603c33b82ddd89bab7c4

My model class:

  import java.util.Date;

import com.univocity.parsers.annotations.Format;
import com.univocity.parsers.annotations.Parsed;

public class Customer {

    @Format(formats ="YYYY-MM-DD")
    @Parsed(field="C_DAY")
    private Date day;

    @Parsed(field="C_ID")
    private Integer id;

    @Parsed(field="C_TYPE")
    private Character type;

    @Format(formats ="YYYY-MM-DD")
    @Parsed(field="C_ORIGIN_DATE")
    private Date originDate;

    @Format(formats ="YYYY-MM-DD")
    @Parsed(field="C_REL_DATE")
    private Date relDate;

    @Parsed(field="C_LEGAL_ID")
    private String legalId;

    @Parsed(field="C_NAME")
    private String name;

Testing output with method:

public void parse(File file) throws IOException {

        CsvParserSettings parserSettings = new CsvParserSettings();
        parserSettings.getFormat().setLineSeparator("\n");
        parserSettings.setHeaderExtractionEnabled(true);
        CsvRoutines routines = new CsvRoutines(parserSettings);

        for (Customer customer : routines.iterate(Customer.class, file, "UTF-8")) {

            System.out.println(customer);


        }
    }
xingbin
  • 27,410
  • 9
  • 53
  • 103
Michael
  • 169
  • 2
  • 2
  • 16
  • 2
    I think your problem stems from the fact that you expect the date to be **printed** in the format you asked it to be parsed from. When you print a `Date` object directly, without a date formatter, you get that format. That doesn't mean it wasn't parsed correctly. – RealSkeptic Oct 02 '18 at 14:42
  • 1
    While I don’t know if Univocity can do it, you should prefer to use `LocalDate` for your dates if you can. It represents a date without time of day, it prints in the format you asked for and it belongs to [java.itme, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 03 '18 at 03:23

0 Answers0