• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative if statement & toString

 
Steve Cooke
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

Monk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic