aspose file tools*
The moose likes Beginning Java and the fly likes Alternative if statement & toString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Alternative if statement & toString" Watch "Alternative if statement & toString" New topic

Alternative if statement & toString

Steve Cooke

Joined: Feb 15, 2006
Posts: 8
I had to write a program for class to check and validate a date is correct. Check if year is between a certain range, check to make sure month is between 1 & 12. Then verify that there is the correct number of days in each month, along with check to see if it's a leap year or not for feb.

The if statements I wrote work but is there a way to clean this up a bit more or an alternative?

Then last part would be the toString. I still havent heard that if the date displays if it needs to be MM/DD/YYYY, or if it can be M/D/YYYY ex 02/02/2000 or 2/2/2000.

I know that using StringBuffer i can append, but how would I add the 0 before the day or month. I'm still tinkering with it, but not seeing any other solutions. I'm just going to assume that the date format needs to be 02/02/2000

Thanks in advance.

MyDate class

MyDateDemo Class
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look into DateFormat and SimpleDateFormat. You can use a DateFormat to parse a string into a Date object. It will throw an exception if the string is not valid. See the setLenient method, too.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
if i were you, i would use a switch statement for every month, it would be
a whole lot easier..

and by the way, ive done that assignment in lab Before lol..


You down with OOP? Yeah you know me!
It is sorta covered in the JavaRanch Style Guide.
subject: Alternative if statement & toString