-2

I receive a date that i want to check if it is not in the right format (dd/MM/YYYY), I add an error to display, knowing my date is of type localDate. I found a lot of code to how to validate string dates but not localdate, if any one has an idea: Here is what I did for the moment and It is rejecting all dates even in the right format

LocalDate mydate;
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY"); 
try {  
        String date = myDate.format(formatter);
        LocalDate.parse(date, formatter);
} 
catch (Exception e) { 
     errors.add(....));
} 
   
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
maya
  • 159
  • 1
  • 12
  • 3
    *FYI:* `"dd/MM/YYYY"` is a bad format pattern. `Y` is *week-based-year*, you want `u` which is *year*, so use `"dd/MM/uuuu"`. See **documentation**, i.e. javadoc of [`DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns). – Andreas Jun 25 '20 at 12:10
  • 1
    What values of `myDate` have you tested? In addition, `mydate` is not the same variable as `myDate` is, the `d`ifference is not directly obvious, admittedly. – deHaar Jun 25 '20 at 12:12
  • *"It is returning all dates even in the right format"* What does that mean? 1) The code is not returning anything. 2) Why would you expect it to not "return" anything for dates in the right format? – Andreas Jun 25 '20 at 12:13
  • 1
    You might have a misunderstanding about what a `LocalDate` object is. A `LocalDate` object is an object that represents a date/month/year. It does not have a format by itself; it's just a date value. It makes no sense to check if a `LocalDate` object "has the right format" - it does not have a format at all. That's why you did not find code to validate a `LocalDate`. – Jesper Jun 25 '20 at 12:18
  • 1
    Does this answer your question? [How to sanity check a date in Java](https://stackoverflow.com/questions/226910/how-to-sanity-check-a-date-in-java) – mohammedkhan Jun 25 '20 at 12:21
  • Please clarify since I cannot make much sense of your question until now, sorry. A `LocalDate` is always valid, otherwise you would not have been able to create it. And a `LocalDate` never has got any format. See for example [How to format LocalDate object to MM/dd/yyyy and have format persist](https://stackoverflow.com/questions/39689866/how-to-format-localdate-object-to-mm-dd-yyyy-and-have-format-persist). – Ole V.V. Jun 25 '20 at 17:02
  • BTW beware of the case of format pattern letters. `YYYY` is not the same as `yyyy`, and the former will probably give you nasty surprises around New Year. Check [the docs](https://docs.oracle.com/javase/10/docs/api/java/time/format/DateTimeFormatter.html) if not quite sure. – Ole V.V. Jun 25 '20 at 17:05

1 Answers1

1

The reasoning seems circular.

You have a value in a LocalDate (I suppose - though you don't show any initialization or assignment to myDate).

You ask the date to format itself as dd/mm/yyyy.

Then you ask LocalDate to parse that same string as dd/mm/yyyy.

I don't see how either of those can fail. The conversion to string must surely succeed if LocalDate was successfully assigned. And of course the parsing of what was just formatted is going to work.

user13784117
  • 1,124
  • 4
  • 4
  • Not a helpful answer. It does fail. You can try it yourself. See the linked original questions for the explanation. – Ole V.V. Nov 20 '22 at 14:25