-3

I receive a string with a date in format: 1990-05-30

  1. I need to convert it to LocalDate

  2. Check that is valid to year/month-day like in my example

  3. And return true or false.

Can you show a good practice?

public boolean check(String date){
    LocalDate localDate = LocalDate.parse(date);
    ...
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
lor lor
  • 121
  • 7

1 Answers1

1

You're almost there. All you need to do now is :

public boolean check(String date){
    try {
        LocalDate localDate = LocalDate.parse(date);
        return true; // valid date if parsing was successful
    } catch (DateTimeParseException e) {
        e.printStackTrace();
        return false; // not a valid date
    }
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57