# Need Help With Leap Year Method

• 09-04-2013, 08:09 PM
giraffasaurus
Need Help With Leap Year Method
This method is driving me crazy :frusty:. It returns day for all cases of Feb. 29th, when it should only return day if it is a leap year, if it is not a leap year 1 should be returned. It works fine for everything except Feb 29th.
Code:

```public int checkDay (int day) {     // For months with 30 days.     if ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 30)         return day;     // For months with 31 days.     if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))         return day;     // For leap years.     // If February 29th...     if (month == 2 && day == 29)     {         // Check if year is a leap year.         if ((year%4 == 0 && year%100!=0) || year%400 == 0)         {             // If year is a leap year return day as 29th.             return day;         }         // If not a leap year, return day as 1st.         else return 1;     }     // If Date if February 1st through 28th return day, as it is valid.     if (month == 2 && (day >= 1 && day <= 28))         return day; // Return day as 1st for all other cases. return 1; }```
• 09-04-2013, 08:24 PM
KevinWorkman
Re: Need Help With Leap Year Method
Have you stepped through this with a debugger, or at least added some print statements to figure out exactly what's going on? Where exactly does the program's behavior start to differ from what you expect? What specific line?
• 09-04-2013, 08:29 PM
giraffasaurus
Re: Need Help With Leap Year Method
Well it has a problem on line 11 of that method. Here when date1 is 2/29/2013 my toString should print 2/1/2013. It works for all other months and for instance is date1 was 2/31/2013 it will print as 2/1/2013. It's just the 29th that's a problem.

Code:

```public class TestDate {         public static void main(String[] args)         {                 Date2 date1 = new Date2(2, 29, 2013);                                 System.out.print("The initail date is: ");                 System.out.print(date1.toString());                                 date1.setMonth(11);                 date1.setDay(1);                 date1.setYear(2003);;                                 System.out.print("\nDate with new value is: ");                 System.out.print(date1.toString());                                 System.out.println();         } }```
• 09-04-2013, 08:35 PM
KevinWorkman
Re: Need Help With Leap Year Method
I don't have access to the Date2 class (and posting it in its entirety is probably a bad idea), so I can't run that code. An SSCCE would make it easier to help you.

I recommend stepping through this with a debugger, or at least adding some print statements, until you narrow the problem down.
• 09-04-2013, 09:56 PM
jim829
Re: Need Help With Leap Year Method
Your said your checkDay routine was wrong. Did you check it out directly?

Regards,
Jim
• 09-05-2013, 05:18 AM
DarrylBurke
Re: Need Help With Leap Year Method