I am given a txt with some specific format. The format is supposed to be starting with dd/MM/uuuu. However i would like to check if it is indeed this specific format or else my code will break.
Now my thoughts were to check if the specific 10 first chars(with the delimeters) can define a LocalDate object. So i came up with this:
public boolean isDate(String date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
LocalDate ld = null;
try {
ld = LocalDate.parse(date, formatter);
System.out.println(ld);
} catch (DateTimeParseException e) {
System.out.println("Date " + date + " is not a date.");
return false;
}
return true;
}
However this is not the best practice because i am controling the programm flow with exceptions. Plus i have to check another 8 fields to be representing time etc.. so my code will be full of try catches. Is there a more effecient way to go around this?