• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date validation

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

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?

Thanks in advance!
Anthony

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the setLenient method.

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.
 
Anthony Andras
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

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?

Thanks
 
Anthony Andras
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pardon my post above, the setLenient method does work when I set it to false. I deeply apologize and thank you for your help.
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic