public interface DateConverter {
String convertFromSourceFormat(String date);
String convertToSourceFormat(String date);
}
public class CustomDateConverter implements DateConverter {
private DateFormat sourceFormat;
private DateFormat targetFormat;
public CustomDateConverter(String sourcePattern, String targetPattern) {
sourceFormat = new SimpleDateFormat(sourcePattern);
targetFormat = new SimpleDateFormat(targetPattern);
}
@Override
public String convertFromSourceFormat(String date) {
try {
Date parsedDate = sourceFormat.parse(date);
return targetFormat.format(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
@Override
public String convertToSourceFormat(String date) {
try {
Date parsedDate = targetFormat.parse(date);
return sourceFormat.format(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
date.converter.class = com.example.CustomDateConverter
date.converter.sourcePattern = yyyy-MM-dd
date.converter.targetPattern = dd/MM/yyyy