The client could be able to send either String
in format
"yyyy-MM-dd HH:mm:ss"
or "yyyy-MM-dd"
and depending on it I need to either just parse full LocalDateTime
if he sent me full format or to create LocalDateTime
object with default Time
part "23:59:59"
For now I have written this solution but it seems to be bad as I am using exceptions for controlling business logic.
public class LocalDateTimeConverter implements IStringConverter<LocalDateTime> {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public LocalDateTime convert(String value) {
LocalDateTime localDateTime;
try {
localDateTime = LocalDateTime.parse(value, DATE_TIME_FORMATTER);
} catch (DateTimeParseException ex) {
localDateTime = LocalDateTime.of(LocalDate.parse(value), LocalTime.of(23, 59, 59));
}
return localDateTime;
}
}
Any suggestions about how to implement it more clearly?