Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date manipulation problem

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Dear friends,

When i execute this code with the date mentioned above in LINE 1, the program never terminates and goes into an infinite loop. But if i change the date in line 1 to "01/03/2008". The program works perfectly and gives the difference betwee the two dates in months.

Kindly provide your help.
Regards,
Vijay
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you start in December of 2007, and keep adding months until you reach March of 2007, of course it's not going to stop. You need to ensure that the start date isn't after the end date.

By the way, I think this

should really be
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ulf,

Thanks for the reply.. But even after changing as you said, the program still goes into an infinite loop if the year is 2007 in Line 1 of the above code.


I changed the equalMonthAndYear as static and made a direct call from checkValidFromDate(). But stil the problem exists.

Please guide me if i had misunderstood anything you had given.

Kindly help me.
Regards,
Vijay
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removing the "new Test()" constructor doesn't help with the problem at hand; that was just something I noticed.

You still need to implement the check for the start being after the end date.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, the problem code is here...



If calc1 doesn't equal calc2, you add a month to calc1 and check again. But what if calc1 is already later than calc2? You can add infinite months to calc1, and it will never equal calc2.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic