package io.ljunggren.csv.parser.mapper;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import io.ljunggren.csv.parser.annotation.CSVDate;
/**
* Mapper that converts a string to a {@link java.util.Date} when the target
* {@link Field} is annotated with {@link CSVDate}. Otherwise delegates to the
* next mapper in the chain.
*/
public class DateMapper extends MapperChain {
@Override
public Object map(Field field, String value) throws Exception {
CSVDate annotation = field.getAnnotation(CSVDate.class);
if (annotation != null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(annotation.pattern());
simpleDateFormat.setLenient(false);
return simpleDateFormat.parse(value);
}
return next(field, value);
}
}