Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Problems with "if " statements in terms of leap years

1. ## Problems with "if " statements in terms of leap years

Hi,
I've been working in my free time on a project which tells what day someone is born based on their birthday. However, when I start to input birthdays greater than about a year old, the program is off by about a day. I believe this problem to be due to the leap year if statement I had, which I bolded in the code below. If you can offer any tips, suggestions, or hints, that would be amazing.

Code:
```import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
public class DayOfBirth {
public static void main (String args[]) {
Scanner scanned = new Scanner (in);
out.println("Which year were you born?");
int birthYear = scanned.nextInt();
out.println("What month were you born?(1 for January, 2 for February...)");
int birthMonth = scanned.nextInt();
out.println("What day were you born?");
int birthDate = scanned.nextInt();

int daysSince = 31 - birthDate;
int monthsSince = 12 - birthMonth;
int yearsSince = 2018 - birthYear;
int monthDaysSince = 0;
switch (monthsSince) {
case 0:
monthDaysSince = 0;//From end of December
break;
case 1:
monthDaysSince = 31;//November
break;
case 2:
monthDaysSince = 61;//October
break;
case 3:
monthDaysSince = 92;//September
break;
case 4:
monthDaysSince = 122;//August
break;
case 5:
monthDaysSince = 153;//July
break;
case 6:
monthDaysSince = 184;//June
break;
case 7:
monthDaysSince = 214;//May
break;
case 8:
monthDaysSince = 245;//April
break;
case 9:
monthDaysSince = 275;//March
break;
case 10:
monthDaysSince = 306;//February
break;
case 11:
monthDaysSince = 334;//January
}
String day = null;
int yearDaysSince = yearsSince * 365;
[B]if (leapYearsSince == 0 && birthYear % 4 == 0 && birthMonth <=2) {
}[/B]
int daysSinceBirth = daysSince + leapYearsSince + monthDaysSince + yearDaysSince;
int daysFromMonday = daysSinceBirth % 7;
switch (daysFromMonday) {
case 0:
day = "Monday";
break;
case 1:
day = "Sunday";
break;
case 2:
day = "Saturday";
break;
case 3:
day = "Friday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Wednesday";
break;
case 6:
day = "Tuesday";
break;
}
out.println("You were born on a " + day + ".");
scanned.close();
}

}```

2. ## Re: Problems with "if " statements in terms of leap years

can you explain this line?
when I start to input birthdays greater than about a year old, the program is off by about a day.

3. ## Re: Problems with "if " statements in terms of leap years

I'm having a bit of trouble following how you are calculating the birthday-of-the-week. Could you explain it (in English rather than code)? Perhaps giving an example for a specific birthday: 1 Jan, 1970 or whatever.