Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#9 A local Joda DateTime PropertyEditor

closed
nobody
None
5
2011-04-08
2011-02-21
Bill Comer
No

Hi there,
First off a great little project thanks.

I use the Joda DateTime classes for all date stuff and I wanted a PropertyEditor for a date field in a CSV.
However i did not want this to be globably available to other classes as I have DateTime Editors with other formats.

There might be a better way but I achieved this with the following:
If there is a better way please enlighten me.

public class CustomCsvToBean<T> extends CsvToBean<T>
{
public CustomCsvToBean() {
addJodaDateTimepropertyEditorToMap();
}

/**
* Add a dateTime PropertyEditor
*/
private void addJodaDateTimepropertyEditorToMap() {
resetEditorMap();

DateTimeFormatter fmt = DateTimeFormat.forPattern("hh:mm:ss dd/MM/yyyy");
PropertyEditor editor = new JodaTimePropertyEditor(fmt);

addEditorToMap(DateTime.class, editor);
}
}

I had to modify CsvToBean slightly:
- opening the scope of addEditorToMap()
- add a function: resetEditorMap

void resetEditorMap()
{
if (editorMap == null)
{
editorMap = new HashMap<Class<?>, PropertyEditor>();
}
}

Discussion

  • Scott Conway
    Scott Conway
    2011-03-02

    Your code looks good. If you have only a single format in a file and you just don't want to do it this way to make the code reusable I would recommend you adding a constructor to your CustomCsvToBean where as an argument you pass in the time format.

    If you have multiple date formats in single file what I would recommend is that you create classes that either contain or inherit DateTime (one for each different format) and create a property editor for each one. This way you can have multiple formats in a single file without a problem.

     
  • Scott Conway
    Scott Conway
    2011-04-08

    • status: open --> closed