Guest User

Untitled

a guest
Jun 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public class Fecha {
  2. private int anio;
  3. private int dia;
  4. private int mes;
  5.  
  6. public Fecha() {
  7. }
  8.  
  9. public Fecha(int anio, int dia, int mes){
  10. setAnio(anio);
  11. setDia(dia);
  12. setMes(mes);
  13. }
  14.  
  15. public int getAnio(){
  16. return anio;
  17. }
  18.  
  19. public int getDia(){
  20. return dia;
  21. }
  22.  
  23.  
  24. public int getMes(){
  25. return mes;
  26. }
  27.  
  28. public void setAnio(int anio){
  29. this.anio=anio;
  30. }
  31.  
  32. public void setDia(int dia){
  33. this.dia=dia;
  34. }
  35.  
  36. public void setMes(int mes){
  37. this.mes=mes;
  38. }
  39.  
  40. public boolean validadFecha(){
  41. boolean validoFecha=false;
  42.  
  43. if (dia<1 || dia>31) {
  44. validoFecha=true;
  45. }
  46.  
  47. if (mes<1 || mes>12) {
  48. validoFecha=true;
  49. }
  50.  
  51. if (mes==2 && dia==29 && anio % 400 == 0 || (anio % 4 == 0 && anio % 100 != 0) ) {
  52. validoFecha=true;
  53. }
  54.  
  55.  
  56.  
  57. return validoFecha;
  58. }
  59. }
  60.  
  61. boolean correcto = false;
  62.  
  63. Fecha fecha = new Fecha(dia, mes, anno);
  64.  
  65. correcto = fecha.validarFecha();
  66.  
  67. if (correcto == true) {
  68. System.out.println("La fecha es correcta");
  69. } else {
  70. System.out.println("La fecha es incorrecta");
  71. }
  72.  
  73. public boolean validarFecha() {
  74. boolean correcto = false;
  75.  
  76. try {
  77. //Formato de fecha (día/mes/año)
  78. SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy");
  79. formatoFecha.setLenient(false);
  80. //Comprobación de la fecha
  81. formatoFecha.parse(this.dia + "/" + this.mes + "/" + this.anno);
  82. correcto = true;
  83. } catch (ParseException e) {
  84. //Si la fecha no es correcta, pasará por aquí
  85. correcto = false;
  86. }
  87.  
  88. return correcto;
  89. }
  90.  
  91. private boolean esFechaValida(int anio, int dia, int mes){
  92. boolean esFechaValida = true;
  93. try{
  94. LocalDate.of(anio, mes, dia);
  95. }catch(DateTimeException e) {
  96. esFechaValida = false;
  97. }
  98. return esFechaValida;
  99. }
Add Comment
Please, Sign In to add comment