Ok so I have a basic Java question, I wasn't sure if I should be placing this in the beginning Java section or this one, so I tried to make my best judgement.
I am currently facing a problem (a sub problem of a much larger assignment for school) where I need to validate a date. In particular, the date Feb 31st is considered to be invalid. The validation I must perform is to determine, based on the day of month entered, if it is a legal day for that particular month.
I have searched in many places looking for suggestions or concepts that might give way to how to go about performing this algorithm (without brute forcing it), and the best lead I have currently goes something like this:
The issue here is that 2010-02-31 seems to parse without throwing the ParseException. Is there something in particular I am missing here or am so far off of the correct answer that I need to consider different approaches?
On a side note, you can remove the "catch exception if invalid" ways by using one of the other parse methods:
This is similar to the code called by DateFormat.parse(String):
By calling parse(String, ParsePosition) directly you prevent the entire exception.
In an attempt to digest your post and experiment with what you have suggested, I have formulated the following code:
In this case, I would expect "fail" to print since there are never 31 days in Feb. However, it prints "success". the setLenient method does not appear to have any impact on the result of the test. I have attempted while setting this property to true, false, and not setting it at all. I admit that my knowledge of the ParsePosition class is not the best so I'm not sure what impact this will have on the method.
Maybe I'm missing something?
posted 9 years ago
Pardon my post above, the setLenient method does work when I set it to false. I deeply apologize and thank you for your help.