Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ValFecha {
- public static void main(String[] args) {
- Scanner entrada = new Scanner(System.in);
- System.out.println("Ingrese fecha dd/mm/yyyy");
- String fecha = entrada.nextLine();
- while(!validarFecha(fecha)) {
- System.out.println("Fecha no valida");
- System.out.println("Ingrese nuevamente la fecha dd/mm/yyyy");
- fecha = entrada.nextLine();
- }
- System.out.println("Fecha CORRECTA");
- }
- public static boolean validarFecha(String fecha) {
- String[] partesFecha = fecha.split("/");
- int dia = Integer.parseInt(partesFecha[0]);
- int mes = Integer.parseInt(partesFecha[1]);
- int anio = Integer.parseInt(partesFecha[2]);
- boolean esValida = true;
- if(fecha.length() >10) {
- esValida = false;
- }
- if (mes < 1 || mes > 12){
- esValida = false;
- } else {
- switch (mes) {
- case 2:
- if (esBisiesto(anio)) {
- if (dia < 1 || dia > 29) {
- esValida = false;
- }
- } else {
- if (dia < 1 || dia > 28) {
- esValida = false;
- }
- }
- break;
- case 4: case 6: case 9: case 11:
- if (dia < 1 || dia > 30) {
- esValida = false;
- }
- break;
- default:
- if (dia < 1 || dia > 31) {
- esValida = false;
- }
- break;
- }
- }
- return esValida;
- }
- public static boolean esBisiesto(int anio) {
- return (anio % 4 == 0 && anio % 100 != 0) || (anio % 400 == 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement