Avatar of mcgettiu
mcgettiu
 asked on

Validating a date object

Hey guys....

Quick question...is it possible to validate a date object?
So i could throw an exception if date is set to something like 31 Feb 2007???

Thanks a mill
Una
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
Ajay-Singh

Try using SimpleDateFormat:

DateFormat format = new SimpleDateFormat("dd MMM yyyy);
format.parse("31 Feb 2007"); // Will throw an exception
KelvinY

Hi mcgettiu,

You can use the isValid() method on the DateTime object.

Regards
  Kelvin
ASKER CERTIFIED SOLUTION
Ajay-Singh

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mcgettiu

ASKER
Ajay-Singh
I already have my date object...so do i have to covert this back to a string to the parse method from DateFormat?

KelvinY
What is the DateTime object?  I cant find it in the Java API?????
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ajay-Singh

> I already have my date object
what do you mean? You have a date object that represents 31 Feb 2007
CEHJ

>>I already have my date object...so do i have to covert this back to a string to the parse method from DateFormat?

Yes (although i don't know how you got the Date in the first place)