Last active
August 29, 2015 14:05
-
-
Save marciojrtorres/2b1a0fe2c520f55f7de8 to your computer and use it in GitHub Desktop.
if indevida e excessivamente aninhado
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 Util { | |
public static int dias(int mes, int ano) { | |
if (mes == 1) { | |
return 31; | |
} else { | |
if (mes == 2) { | |
if (ano % 400 == 0) { | |
return 29; | |
} else { | |
if (ano % 4 == 0 && ano % 100 > 0) { | |
return 29; | |
} else { | |
return 28; | |
} | |
} | |
} else { | |
if (mes == 3) { | |
return 31; | |
} else { | |
if (mes == 4) { | |
return 30; | |
} else { | |
if (mes == 5) { | |
return 31; | |
} else { | |
if (mes == 6) { | |
return 30; | |
} else { | |
if (mes == 7) { | |
return 31; | |
} else { | |
if (mes == 8) { | |
return 31; | |
} else { | |
if (mes == 9) { | |
return 30; | |
} else { | |
if (mes == 10) { | |
return 31; | |
} else { | |
if (mes == 11) { | |
return 30; | |
} else { | |
if (mes == 12) { | |
return 31; | |
} else { | |
throw new IllegalArgumentException("mes invalido"); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println(Util.dias(2, 2016)); // 29 | |
} | |
} |
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 Util { | |
public static int dias(int mes, int ano) { | |
if (mes == 1 || mes == 3 || mes == 5 || mes == 7 || mes == 8 || mes == 10 || mes == 12) return 31; | |
else if (mes == 2) | |
if (ano % 400 == 0) return 29; | |
else if (ano % 4 == 0 && ano % 100 > 0) return 29; | |
else return 28; | |
else if (mes == 4 || mes == 6 || mes == 9 || mes == 11) return 30; | |
throw new IllegalArgumentException("mes invalido"); | |
} | |
public static void main(String[] args) { | |
System.out.println(Util.dias(2, 2016)); // 29 | |
} | |
} |
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 Util { | |
private static final int[] diasMes = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
private static final int FEVEREIRO = 2; | |
private static boolean ehBissexto(int ano) { | |
return ano % 400 == 0 || (ano % 4 == 0 && ano % 100 != 0); | |
} | |
private static boolean mesInvalido(int mes) { | |
return mes >= 1 && mes <= 12; | |
} | |
public static int dias(int mes, int ano) { | |
if (mesInvalido(mes)) throw new IllegalArgumentException("mes invalido"); | |
if (mes == FEVEREIRO && ehBissexto(ano)) return 29; | |
return diasMes[mes - 1]; | |
} | |
public static void main(String[] args) { | |
System.out.println(Util.dias(2, 2016)); // 29 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment