How to restrict the values of a date in a field of a form in java?

2

I have a form built with Swing (Java) that has some fields, among them there is one that is the Birth Date (a String that receives the value of a date and has a formatted field ##/##/#### .) is represented by the variable jTFDataNascimento which is a JFormattedTextField .

How could you prevent the user from placing absurd dates such as: 11/20/2090 or 11/10/1890 ?

The only validation I make is to check if the fields are empty:

if(jTFDataNascimento.getText().equals("  /  /    "))...

I also wanted to validate the dates as to their values and not validate only if the content is empty or not, is it possible?

There are no restrictions on whether the field is a String or a date, so I put String for convenience!

I have already worked on some java web projects and doing this validation is something trivial on a html5 page, everything I learned about dates in java is about the java7 version, I think in java8 this is simpler to do.

I think there should be a code that receives a date and check if it is less than a date previously entered in the system and also see if that date is larger than the earlier date previously registered. or better still
the method receives three dates (currentTime, dataRemota, DateTime) and checks whether theTimeTimeTime is less than the currentTimeTime or if thatTimeTime is greater than theTimeTimeTimeTime.

public boolean validarData(LocalDate dataAtual, LocalDate dataRemota, LocalDate dataNascimento){
    //codigo que não sei ainda...
    return false;
}
    
asked by anonymous 27.03.2016 / 20:00

1 answer

2

You can use two ways to make this comparison, with the new java8 class, at LocalDate , or the traditional Date / a>:

Using LocalDate :

public class CompararDataTeste {

    public static boolean validarDataLocalDate(LocalDate dataAtual, LocalDate dataRemota, LocalDate dataNascimento) {

        return dataNascimento.isAfter(dataRemota) || dataNascimento.isBefore(dataAtual);
}

public static void main(String[] args) throws ParseException {
    LocalDate localDataAtual = LocalDate.now();
    LocalDate localDataNascimento = LocalDate.of(1998, 3, 27);
    LocalDate localDataRemota = LocalDate.of(1990, 1, 1);

    System.out.println(validarDataLocalDate(localDataAtual, localDataRemota, localDataNascimento));

        }
    }

Already with Date , it would look like this:

public class CompararDataTeste {

    public static boolean validarDataDate(Date dataAtual, Date dataRemota, Date dataNascimento) {

        return dataNascimento.after(dataRemota) || dataNascimento.before(dataAtual);
}

public static void main(String[] args) throws ParseException {
    Date dataRemota = new SimpleDateFormat("dd/mm/yyyy").parse("01/01/1990");
    Date dataNascimento = new SimpleDateFormat("dd/mm/yyyy").parse("27/03/1998");
    Date dataAtual = new Date();
    System.out.println(validarDataDate(dataAtual, dataRemota, dataNascimento));

        }
    }

Both examples will return true , since dataNascimento is later than dataRemota , and prior to dataAtual . You can see both examples working right here at IDEONE .

It was not very clear in the question whether both conditions need to be true to be a valid date, in this case, just replace the || operator with && .

Addendum

If you get a String and want to convert to LocalDate , in addition to the date itself, you need to do something like this for the Date class, create DateTimeFormatter and parse the two information as arguments:

String stringData = "27/03/2016";
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate sData = LocalDate.parse( stringData , fmt2 );

As you are using textFields , you will need to merge the above example before moving your date to the already-exemplified method.

References:

How to compare LocalDate instances Java 8

The JAVA 8 date API

LocalDate - Documentation

    
28.03.2016 / 02:35