File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes learning how to use classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "learning how to use classes" Watch "learning how to use classes" New topic
Author

learning how to use classes

Joseph Swager
Ranch Hand

Joined: Feb 04, 2010
Posts: 41

Hello all. Hope this isnt a dumb question. I'm writing code to compare two dates and list the difference in months between dates.
works for the year but i keep getting the same answer for the months. example june 1980 and then the date to compare it to may 1982

the answer will be 23 months. but my code would return 24.
then if i enter the same for the first date then January 1984 then it would be 42 but mine will return 48

I commented where i think i went wrong but if anyone can tell me where i went wrong please let me know. its driving me crazy.



"Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do." -- Steve Jobs
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Just scanning through the code, I could spot one potential problem: In the getMonth() method, you are taking String "mon" as an argument, but then you are not using it anywhere in the method, instead you are comparing the class-level "month" variable value with String month names. So, the value "newMonth" obtained in the main method is getting lost in the process, since the months you compare are always the same (i.e. both are values got from class level variable "month"). This is probably the reason why your code only finds difference in the years entered, and then multiplies it by 12 and gives the answer in number of months and you are getting answers like 12 months, 24 months, 48 months etc.

Other than this, in the monthsUtil method, I find the logic to calculate the "diff" a bit weird. For example, if you choose January 2000 and June 2001 as your two dates, then the expression

would give the answer as 7, which is incorrect. I guess you would need to work on this logic a bit.
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Another question is why are you asking for the "day" only once? If you want to compare the dates by considering the day-of-month, then you should also ask for the day value to be compared. Then you would also need to modify the logic for calculating the "diff" (mentioned in my previous reply) by considering the day along with the month and the year values.
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Oh, and Welcome to Javaranch!
Joseph Swager
Ranch Hand

Joined: Feb 04, 2010
Posts: 41

Thank you so far for the answers. Oh and i got rid of day from the program. sorry for the confusion.
Everything works the way i want it but when i need to do the diffrence math on the month.






so lets say when i'm promted for d1 info and i enter June 1980

and now im entering info to compare so i enter July 1981

now the diff should be 13 but its coming back 12.


im passing in the new dates to a method and using it to d1 with




only problem i am having is theMonth and the month always seem to have the same value. Its like the newMonth value im passing is getting d1 value no matter what.

I just want to know why this wouldnt work


Joseph Swager
Ranch Hand

Joined: Feb 04, 2010
Posts: 41

Yeah i see the logic problem thank you.
Joseph Swager
Ranch Hand

Joined: Feb 04, 2010
Posts: 41

Hey Pushkar Choudhary,
You where right on the math i changed the - to + and it works

also i was forgetting to add a string var in getMonth ( string month) method. here is the final code and it works like i wanted. thank you thank you.
I'm loving to learn Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: learning how to use classes