public static boolean validDate(String dateString) { return DateUtil.parse(dateString) != null; // эта строка } 

Here is the parse (String) method from the DateUtil class:

 public static LocalDate parse(String dateString) { try { return DATE_FORMATTER.parse(dateString, LocalDate::from); } catch (DateTimeParseException e) { return null; } } 
  • 2
    in the parse method, the logic is such that if an exception is thrown when trying to parse, null is returned, and the validDate method looks at the result of the parse method, and if null is returned, the date is not parsed, we return false - DaysLikeThis

2 answers 2

The method itself is of type boolean. Therefore, I think the return true; string is return true; You should not have been surprised. The expression DateUtil.parse(dateString) != null returns just Boolean.

This whole "suspicious string" is equivalent to

 if (DateUtil.parse(dateString) != null) { return true; } else { return false; } 

but only the very beginners write.

     DateUtil.parse(dateString) != null 

    returns true if parse() returns non- null , otherwise false

    the point is that if the string is parsed before the date, the function will return ok, yes this string contains some date