How to introduce a variable date formatting in BeanIO xml file?

goe picture goe · Mar 2, 2012 · Viewed 8.6k times · Source

Here's my BeanIO xml configuration file:

<beanio xmlns="http://www.beanio.org/2011/01"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.beanio.org/2011/01 http://www.beanio.org/2011/01    /mapping.xsd">
  <stream name="Test" format="delimited">
    <record name="TestRow" minOccurs="1" maxOccurs="unbounded" class="com.company.TestRow">
      <field name="transactionDate" type="date" format="MM/dd/yyyy"/>
      <field name="userId" type="string"/>
      <field name="clientName" type="string"/>
    </record>
  </stream>
</beanio>

The issue with it is that I need the "MM/dd/yyyy" to be dynamically set by the class that calls this xml file to parse the file. Because the date format is dependent on the user setup.

Can that be done somehow?

Answer

Srinivas picture Srinivas · Mar 2, 2013

Try this, it should work.

Define a type handler for default DateTypeHandler in the mapping file.

<typeHandler name="dateTypeHandler" class="org.beanio.types.DateTypeHandler" />

Use that handler on your field. That is all you need.

<field name="transactionDate" typeHandler="dateTypeHandler" format="MM/dd/yyyy"/>