2

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?

Michel Keijzers
  • 13,956
  • 24
  • 83
  • 111
goe
  • 211
  • 1
  • 4
  • 15

4 Answers4

4

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"/>
Srinivas
  • 61
  • 1
3

The should work, but is definitely a hack. First, create a custom type handler like this:

package example;
import org.beanio.types.DateTypeHandler;

public class ClientDateTypeHandler extends DateTypeHandler {
    private static ThreadLocal<String> datePattern = new ThreadLocal<String>();

    public ClientDateTypeHandler() {
        setPattern(datePattern.get());
    }

    public static void setDatePattern(String s) {
        datePattern.set(s);
    }
}

Then register the type handler in your mapping file:

<typeHandler type="java.util.Date" class="example.ClientDateTypeHandler" />

And finally, call ClientDateTypeHandler.setDatePattern(...) before using a StreamFactory to load your mapping file.

Interesting use case, I didn't think of that.

Kevin
  • 31
  • 1
1

One more example:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.beanio.types.TypeConversionException;
import org.beanio.types.TypeHandler;

import com.google.common.base.Strings;

public class TimestampHandler implements TypeHandler {
private SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyyy");

@Override
public Object parse(String text) throws TypeConversionException {
    if (Strings.isNullOrEmpty(text)) {
        return null;
    }
    try {
        return dateFormat.parse(text);
    } catch (ParseException ex) {
        throw new TypeConversionException(ex);
    }
}

@Override
public String format(Object value) {
    if (value == null || value.toString().isEmpty()) {
        return "";
    }
    return dateFormat.format(value);
}

@Override
public Class<?> getType() {
    return java.sql.Timestamp.class;
}

}
jeton
  • 771
  • 10
  • 15
0

I suppose you're looking for java.sql.Timestamp to java.lang.String handler. A little bit a cleaner version:

import java.sql.Timestamp;
import java.util.Date;

import org.beanio.types.DateTypeHandlerSupport;
import org.beanio.types.TypeConversionException;

public class TimestampTypeHandler extends DateTypeHandlerSupport {

    public TimestampTypeHandler() { }

    public TimestampTypeHandler(String pattern) {
        super(pattern);
    }

    @Override
    public Object parse(String text) throws TypeConversionException {
        if (text == null || text.isEmpty()) {
            return null;
        }
        return new Timestamp(super.parseDate(text).getTime());
    }

    @Override
    public String format(Object value) {
        Date dateTime = (value == null) ? null : new Date(((Timestamp)value).getTime());
        return super.formatDate(dateTime);
    }

    @Override
    public Class<?> getType() {
        return Timestamp.class;
    }

}

Configuration for it is as easiest as other type handlers:

<typeHandler type="java.sql.Timestamp" class="TimestampTypeHandler">
  <property name="pattern" value="MM/dd/yyyy" />
</typeHandler>

Here is unit test provided: Gist for TimestampTypeHandlerTest

shx
  • 780
  • 1
  • 11
  • 27