How to validate Date?

1

Good morning,

The next thing is, I have a Date class that handles day, month, year, as a whole, each one, and in that class, a method that is ValidateDate. The question is that the date is entered by keyboard in the main method and it is received by a class X that has a variable attribute of type Date, as I operate the method Validate Date of the class Date.

public class Fecha {
    private int anio;
    private int dia;
    private int mes;

    public Fecha() {
    }

    public Fecha(int anio, int dia, int mes){
        setAnio(anio);
        setDia(dia);
        setMes(mes);
    }

    public int getAnio(){
        return anio;
    }

    public int getDia(){
        return dia;
    }


    public int getMes(){
        return mes;
    }

    public void setAnio(int anio){
        this.anio=anio;
    }

    public void setDia(int dia){
        this.dia=dia;
    }

    public void setMes(int mes){
        this.mes=mes;
    }

    public boolean validadFecha(){
        boolean validoFecha=false;

        if (dia<1 || dia>31) {
            validoFecha=true;
        }

        if (mes<1 || mes>12) {
            validoFecha=true;
        }

        if (mes==2 && dia==29 && anio % 400 == 0 || (anio % 4 == 0 && anio % 100 != 0) ) {
            validoFecha=true;
            }



        return validoFecha;
        }
}
    
asked by MZ39720 21.06.2018 в 08:54
source

2 answers

1

Assuming that in the Main class you are asking the user to enter the date in three steps (day, month and year) and that you are using a "constructor" in the Date class to start the variables dia , mes and anio , you should do this ...

In the Main class, after requesting the date, call class Fecha and method validarFecha()

boolean correcto = false;

Fecha fecha = new Fecha(dia, mes, anio);

correcto = fecha.validarFecha();

if (correcto == true) {
    System.out.println("La fecha es correcta");
} else {
    System.out.println("La fecha es incorrecta");
}

If you want to improve your code you should use this in your class Fecha :

public boolean validarFecha() {
    boolean correcto = false;

    try {
        //Formato de fecha (día/mes/año)
        SimpleDateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy");
        formatoFecha.setLenient(false);
        //Comprobación de la fecha
        formatoFecha.parse(this.dia + "/" + this.mes + "/" + this.anio);
        correcto = true;
    } catch (ParseException e) {
        //Si la fecha no es correcta, pasará por aquí
        correcto = false;
    }

    return correcto;
}

Try to see

    
answered by 21.06.2018 / 09:58
source
1

The easy thing is to try to convert to LocalDate and capture the exception. A regular expression would allow you to validate the correction of the fields, but could not verify on February 29.

An example

    private boolean esFechaValida(int anio, int dia, int mes){
        boolean esFechaValida = true;
        try{
            LocalDate.of(anio, mes, dia);
        }catch(DateTimeException e) {
            esFechaValida = false;
        }
        return esFechaValida;
    }
    
answered by 21.06.2018 в 09:57