Handling Empty Dates

161 views
Skip to first unread message

hoffmandirt

unread,
Oct 22, 2008, 10:10:50 AM10/22/08
to Fixedformat4j User List
What is the best way to handle empty date fields? For example I have a
file that has a date/time, the format is yyyy/MM/ddHH:mm, that
represents a cancelation. Most of the time this date/time is not
there. Even worse, when it's not there, it comes through as:

data_prior_to_cancelation_date_time / / :
data_after_cancelation_date_time

I think the ByTypeFormatter is what I need to user here. I am going
to give that a shot. Great tool by the way!

Jacob von Eyben

unread,
Oct 22, 2008, 5:01:01 PM10/22/08
to Fixedformat4j User List
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

hoffmandirt

unread,
Oct 23, 2008, 7:56:35 AM10/23/08
to Fixedformat4j User List
This is exactly what I needed. I am running into the same issue with
integers and I was assuming that if I changed my type from int to
Integer that your tool would just set the value to null. Either way, I
can write a formatter for this too. Thanks again!

On Oct 22, 5:01 pm, Jacob von Eyben <jacobvoney...@gmail.com> wrote:
> 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(s­tring);
>       } 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
> On Oct 22, 4:10 pm, hoffmandirt <dpoli...@gmail.com> wrote:
>
>
>
> > What is the best way to handle empty date fields? For example I have a
> > file that has a date/time, the format is yyyy/MM/ddHH:mm, that
> > represents a cancelation. Most of the time this date/time is not
> > there. Even worse, when it's not there, it comes through as:
>
> > data_prior_to_cancelation_date_time    /  /    :
> > data_after_cancelation_date_time
>
> > I think the ByTypeFormatter is what I need to user here.  I am going
> > to give that a shot. Great tool by the way!- Hide quoted text -
>
> - Show quoted text -

hoffmandirt

unread,
Oct 23, 2008, 7:56:35 AM10/23/08
to Fixedformat4j User List
This is exactly what I needed. I am running into the same issue with
integers and I was assuming that if I changed my type from int to
Integer that your tool would just set the value to null. Either way, I
can write a formatter for this too. Thanks again!

On Oct 22, 5:01 pm, Jacob von Eyben <jacobvoney...@gmail.com> wrote:
> 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(s­tring);
>       } 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
> On Oct 22, 4:10 pm, hoffmandirt <dpoli...@gmail.com> wrote:
>
>
>
> > What is the best way to handle empty date fields? For example I have a
> > file that has a date/time, the format is yyyy/MM/ddHH:mm, that
> > represents a cancelation. Most of the time this date/time is not
> > there. Even worse, when it's not there, it comes through as:
>
> > data_prior_to_cancelation_date_time    /  /    :
> > data_after_cancelation_date_time
>
> > I think the ByTypeFormatter is what I need to user here.  I am going
Reply all
Reply to author
Forward
0 new messages