Created
March 23, 2019 15:31
-
-
Save SiAust/921fe3ce9b1a0158f6a69aec57bb0ea2 to your computer and use it in GitHub Desktop.
Takes an input year and returns whether the year is a leap year.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumberOfDaysInMonth { | |
public static boolean isLeapYear(int year){ | |
boolean isLeapYear = false; | |
if (year < 1 || year > 9999){ | |
return false; | |
} | |
if (year % 4 == 0){ | |
if (year % 100 == 0){ | |
isLeapYear = false; | |
} else { | |
isLeapYear = true; | |
} | |
} | |
if (year % 400 == 0) { | |
isLeapYear = true; | |
} | |
return isLeapYear; | |
} | |
public static int getDaysInMonth(int month, int year){ | |
int days = 0; | |
if (month < 1 || month >12){ | |
return -1; | |
} | |
if (year < 1 || year > 9999){ | |
return -1; | |
} | |
if (isLeapYear(year)){ | |
switch (month){ | |
case 1: case 3: case 5: case 7: case 8: case 10: case 12: | |
days = 31; | |
break; | |
case 2: | |
days = 29; | |
break; | |
case 4: case 6: case 9: case 11: | |
days = 30; | |
break; | |
} | |
} else { | |
switch (month){ | |
case 1: case 3: case 5: case 7: case 8: case 10: case 12: | |
days = 31; | |
break; | |
case 2: | |
days = 28; | |
break; | |
case 4: case 6: case 9: case 11: | |
days = 30; | |
break; | |
} | |
} | |
return days; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment