The ByTypeFormatter simply detects what formatter to use from the
datatype returned from the getter field. In your case the formatting
will be performed by the
com.ancientprogramming.fixedformat4j.format.impl.DateFormatter.
I would probably create my own Custom formatter as your representation
of an empty Date is a "special case" in fixedformat4j terms.
You will do that by implementing the FixedFormatter interface and
implement the parse and format methods.
I have not tested the following and it should only be seen as a
inspiration. I have simply copied and modifed the DateFormatter
implementation (
http://code.google.com/p/fixedformat4j/source/browse/
trunk/fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/
format/impl/DateFormatter.java?r=67) so the Alignment isn´t used.
Further on I have implemented your special empty string test:
public class CustomDateFormatter implements FixedFormatter<Date> {
public Date parse(String string, FormatInstructions instructions)
throws FixedFormatException {
Date result = null;
//test that the date isn't empty according to our special empty
string representation
if (! " / / : ".equals(string)) {
try {
result =
getFormatter(instructions.getFixedFormatPatternData().getPattern()).parse(string);
} catch (ParseException e) {
throw new FixedFormatException("Could not parse value[" +
string + "] by pattern[" +
instructions.getFixedFormatPatternData().getPattern() + "] to " +
Date.class.getName());
}
}
return result;
}
public String asString(Date date, FormatInstructions instructions) {
String result = null;
if (date != null) {
result =
getFormatter(instructions.getFixedFormatPatternData().getPattern()).format(date);
}
return result;
}
DateFormat getFormatter(String pattern) {
return new SimpleDateFormat(pattern);
}
}
And here is how you would use your new formatter
@Field(offset = 1, length = 15, formatter = CustomDateFormatter.class)
@FixedFormatPattern("yyyy/MM/ddHH:mm")
public Date getDateData() {
...
}
I hope it helped you.
Regards Jacob