aspose file tools*
The moose likes Beginning Java and the fly likes Date Comparison Program - Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date Comparison Program - Help!" Watch "Date Comparison Program - Help!" New topic
Author

Date Comparison Program - Help!

Chris Hathaway
Greenhorn

Joined: Feb 08, 2012
Posts: 16
Hi, I am currently developing a program that will allow you to enter in two dates and it will tell you the difference between them. Im currently having problems getting the months to properly work, the way I want it to work is that the user enters in the month(such as january or april) for each date, and a year for each date(such as 2012 or 1776). The year difference works fine, but for the months I cant seem to figure it out, any help would be greatly appreciated! Here is my code so far!
-----------------------------------------------------------------------------------------



-----------------------------------------------------------------------------------------

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Can you give an example of an input where the code does not do what you expected it to do? I see that for years you're handling the case where one value might be smaller or larger than the other, whereas for months you don't do that. Maybe start by fixing that.

Once you're a bit further down the road, check out the java.text.SimpleDateFormat class - it can convert characters strings containing month names to numbers much more easily than what you're doing here.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8159
    
  23

Chris Hathaway wrote:Im currently having problems getting the months to properly work...

What Tim said. Are you familiar with methods yet? Because you could use them to eliminate a lot of the repeated code in your program.
Also, for calculation purposes, you may find it easier to number your months from 0 to 11. For one thing, you can then use them as indexes, and for the calculations themselves you probably won't have to add '±1' as often.

If this isn't just an exercise, you may also want to look at Joda Time; I'm pretty certain that its Interval class will already do everything you want.

Oh, and BTW, I really like your field naming. Very clear and easy to follow. For true adherence to convention, you might want to get into the habit of using camel case (eg, monthNumberTwo, rather than monthnumbertwo), but other than that it's very good; and you'd be surprised how many newbies get that part wrong. Keep it up.


Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Comparison Program - Help!