revised: calculating date objects

okay, I need to count the number of days between two dates, input with separate prompts; currently, if I put in 1/1/2000 for the starting date and 1/1/2001 for the ending date I get 373, not 366 like I should. can anyone see where I am going wrong?

int sYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting year:"));

int sMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting month:"));

int sDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting day:"));

//end year

int eYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending year:"));

int eMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending month:"));

int eDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending day:"));

int counter = 0;

int curDay = sDay;

int curMonth = sMonth;

int curYear = sYear;

//curDay != eDay && curMonth != eMonth &&

while (curYear != eYear){

counter ++;

//this is the leap year stuff

boolean leapYear =false;

if((eYear%4 == 0 && eYear%100 !=0) || eYear%400 == 0){

leapYear =true;

}//if

int monthDays = 0;

switch (eMonth){

case 1: monthDays = 31;

break;

case 2:if(leapYear){

monthDays = 29;

}//if

else{

monthDays = 28;

}//else

break;

case 3: monthDays = 31;

break;

case 4: monthDays = 30;

break;

case 5: monthDays = 31;

break;

case 6: monthDays = 30;

break;

case 7: monthDays = 31;

break;

case 8: monthDays = 31;

break;

case 9: monthDays = 30;

break;

case 10: monthDays = 31;

break;

case 11: monthDays = 30;

break;

case 12: monthDays = 31;

break;

}//end switch

if(curDay > monthDays){

curMonth ++;

curDay = 1;

}

if(curMonth > 12){

curYear ++;

curMonth = 1;

}

curDay++;

}

JOptionPane.showMessageDialog(null,"here it is : " + counter);

System.exit(0);

}

}

[4458 byte] By [kalugaa] at [2007-11-26 19:52:42]
# 1
Could you not just use the Calendar classes?
dcmintera at 2007-7-9 22:43:46 > top of Java-index,Java Essentials,New To Java...
# 2

If it's part of your homework to do all that calendar-fiddling, then you never change eMonth so you treat every month as if it had 31 days.

If it isn't part of your homework, then where you are going wrong is doing all that calendar-fiddling instead of using Calendar objects to do those calculations.

DrClapa at 2007-7-9 22:43:46 > top of Java-index,Java Essentials,New To Java...
# 3

> If it's part of your homework to do all that

> calendar-fiddling, then you never change eMonth so

> you treat every month as if it had 31 days.

>

> If it isn't part of your homework, then where you are

> going wrong is doing all that calendar-fiddling

> instead of using Calendar objects to do those

> calculations.

can't use calendar objects. still confused; why do I want to treat every month as having 31 days? obviously, that would be incorrect

kalugaa at 2007-7-9 22:43:46 > top of Java-index,Java Essentials,New To Java...
# 4
> If it's part of your homework to do all that> calendar-fiddling, then you never change eMonth so> you treat every month as if it had 31 days.He shouldn't "change eMonth", though. He should "switch on curMonth".
doremifasollatidoa at 2007-7-9 22:43:46 > top of Java-index,Java Essentials,New To Java...
# 5

> > If it's part of your homework to do all that

> > calendar-fiddling, then you never change eMonth so

> > you treat every month as if it had 31 days.

>

> He shouldn't "change eMonth", though. He should

> "switch on curMonth".

okay, that is what I thought. let me try that and I'll get back to ya. thanks

kalugaa at 2007-7-9 22:43:46 > top of Java-index,Java Essentials,New To Java...
# 6

> > If it's part of your homework to do all that

> > calendar-fiddling, then you never change eMonth so

> > you treat every month as if it had 31 days.

>

> He shouldn't "change eMonth", though. He should

> "switch on curMonth".

that seems to work, thanks a ton

kalugaa at 2007-7-9 22:43:46 > top of Java-index,Java Essentials,New To Java...
# 7

You'll also need to fix this:

//curDay != eDay && curMonth != eMonth &&

while (curYear != eYear){

Otherwise, as soon as you hit eYear, you quit. That won't work unless the end date is January 1 of whatever year.

doremifasollatidoa at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...
# 8

> You'll also need to fix this:

> //curDay != eDay && curMonth != eMonth &&

> while (curYear != eYear){

>

> Otherwise, as soon as you hit eYear, you quit. That

> won't work unless the end date is January 1 of

> whatever year.

hmm, I am not quite sure what you mean; can anyone clarify?

kalugaa at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...
# 9

okay, I have run some more tests on the code and realized that I still have problems: i.e., I entered start date as 4/1/2001 and end as 4/1/2002 and got 276 as the # of days. I am really lost, can anyone see the problem?

int sYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting year:"));

int sMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting month:"));

int sDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting day:"));

//end year

int eYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending year:"));

int eMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending month:"));

int eDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending day:"));

int counter = 0;

int curDay = sDay;

int curMonth = sMonth;

int curYear = sYear;

//curDay != eDay && curMonth != eMonth &&

while (curYear != eYear){

counter ++;

//this is the leap year stuff

boolean leapYear = false;

if((eYear%4 == 0 && eYear%100 !=0) || eYear%400 == 0){

leapYear = true;

}//if

int monthDays = 0;

switch (curMonth){

case 1: monthDays = 31;

break;

case 2: if(leapYear){

monthDays = 29;

}//if

else{

monthDays = 28;

}//else

break;

case 3: monthDays = 31;

break;

case 4: monthDays = 30;

break;

case 5: monthDays = 31;

break;

case 6: monthDays = 30;

break;

case 7: monthDays = 31;

break;

case 8: monthDays = 31;

break;

case 9: monthDays = 30;

break;

case 10: monthDays = 31;

break;

case 11: monthDays = 30;

break;

case 12: monthDays = 31;

break;

}//end switch

if(curDay > monthDays){

curMonth ++;

curDay = 1;

}

if(curMonth > 12){

curYear ++;

curMonth = 1;

}

curDay++;

}

JOptionPane.showMessageDialog(null, "here it is : " + counter);

System.exit(0);

}

}

kalugaa at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...
# 10

> okay, I have run some more tests on the code and

> realized that I still have problems: i.e., I entered

> start date as 4/1/2001 and end as 4/1/2002 and got

> 276 as the # of days. I am really lost, can anyone

> see the problem?

That's exactly what I was talking about before (in my Reply #7): you need to continue your loop until your curMonth/curDay/curYear all match the corresponding eMonth/eDay/eYear.

doremifasollatidoa at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...
# 11
if your end date is 2/2/2007,the loop will quit at 1/1/2007, as it satisfy the loop exit condition curYear==eYear, thus not counting 1/1 to 2/2
Icycoola at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...
# 12
> if your end date is 2/2/2007,> the loop will quit at 1/1/2007, as it satisfy the> loop exit condition curYear==eYear, thus not counting> 1/1 to 2/2Exactly what I was telling him!
doremifasollatidoa at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...
# 13

> > if your end date is 2/2/2007,

> > the loop will quit at 1/1/2007, as it satisfy the

> > loop exit condition curYear==eYear, thus not

> counting

> > 1/1 to 2/2

>

> Exactly what I was telling him!

okay, okay, I see what you are saying now; here is what I have now, but it still has some problems; like sometimes it just gets into an infinite loop and I have debugged through it so many times and I can't see why. anyone see where I could be going wrong? BTW, this is just a small part of a program that I have to have done by 12 noon tomorrow is anyone going to be up for while that could check this post periodically for code that I post? there are some major duke points in it for ya . . .

//beg date

int sYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting year:"));

int sMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting month:"));

int sDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting day:"));

//end year

int eYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending year:"));

int eMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending month:"));

int eDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the ending day:"));

//cancel date input

int cYear = Integer.parseInt(JOptionPane.showInputDialog("Enter the cancel year:"));

int cMonth = Integer.parseInt(JOptionPane.showInputDialog("Enter the cancel month:"));

int cDay = Integer.parseInt(JOptionPane.showInputDialog("Enter the cancel day:"));

int counter = 0;

int curDay = sDay;

int curMonth = sMonth;

int curYear = sYear;

boolean done = true;

while (done){

counter ++;

//this is the leap year stuff

boolean leapYear = false;

if((eYear%4 == 0 && eYear%100 !=0) || eYear%400 == 0){

leapYear = true;

}//if

int monthDays = 0;

switch (curMonth){

case 1: monthDays = 31;

break;

case 2: if(leapYear){

monthDays = 29;

}//if

else{

monthDays = 28;

}//else

break;

case 3: monthDays = 31;

break;

case 4: monthDays = 30;

break;

case 5: monthDays = 31;

break;

case 6: monthDays = 30;

break;

case 7: monthDays = 31;

break;

case 8: monthDays = 31;

break;

case 9: monthDays = 30;

break;

case 10: monthDays = 31;

break;

case 11: monthDays = 30;

break;

case 12: monthDays = 31;

break;

}//end switch

if(curDay > monthDays){

curMonth ++;

curDay = 1;

}

if(curMonth > 12){

curYear ++;

curMonth = 1;

}

if (curYear == eYear && curMonth == eMonth && curDay == eDay) {

done = false;

}

curDay++;

}

counter--;

JOptionPane.showMessageDialog(null, "here it is : " + counter);

System.exit(0);

}

}

kalugaa at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...
# 14

Can you tell us what input leads to an infinite loop? I can see if you input invalid dates(31/2/2006) it will go infinite loop. To solve that you may want to do some input check (aka block the operation if invalid input detected)

Side note: for leap year the curYear should be checked, but not the eYear.If eYear is lear year doesn't mean all years are ya know ;p

Edit: Try to avoid using System.exit as much as possible, you don't want your program to "quit" when anything happens yeah? (hint: some program starts with w ;p)

Message was edited by:

Icycool

Icycoola at 2007-7-9 22:43:47 > top of Java-index,Java Essentials,New To Java...