• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Alternative if statement & toString

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic