0

I'm attempting to parse a date from CSV like this:

2016-03-01

To a Joda DateTime with SuperCSV Dozer, like this:

private static final String[] FIELD_MAPPING = new String[] {"date"};

final CellProcessor[] processors = new CellProcessor[] {
    new ParseDateTime(DateTimeFormat.forPattern("YYYY-MM-DD"))
};

CsvDozerBeanReader beanReader = new CsvDozerBeanReader(
    new FileReader("/path/to.csv"), CsvPreference.STANDARD_PREFERENCE);
beanReader.configureBeanMapping(MyDateHoldingBean.class, FIELD_MAPPING);
MyDateHoldingBean bean = beanReader.read(EmployeeDetails.class, processors)

The DateTime returned is the current date & time, not a representation of the date read from CSV.

Am I doing it wrong?

Polk
  • 1
  • 1

1 Answers1

0

You're missing a step; you need to configure the Dozer mapping. Currently this must be done with a DozerBeanMapper:

final CellProcessor[] processors = new CellProcessor[] {
    new ParseDateTime(DateTimeFormat.forPattern("yyyy-MM-dd"))
};

DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(new FileInputStream("/path/to/dozer.xml"));

CsvDozerBeanReader beanReader = new CsvDozerBeanReader(new FileReader("/path/to.csv"), 
    CsvPreference.STANDARD_PREFERENCE, mapper);
MyDateHoldingBean bean = beanReader.read(MyDateHoldingBean.class, processors)

where dozer.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://dozer.sourceforge.net
      http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping>
        <class-a>org.supercsv.io.dozer.CsvDozerBeanData</class-a>
        <class-b>your.package.MyDateHoldingBean</class-b>
        <field copy-by-reference="true">
            <a>columns[0]</a>
            <b>date</b>
        </field>
   </mapping>
</mappings>

See this question

Community
  • 1
  • 1
BugOrFeature
  • 327
  • 1
  • 6
  • 13