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

Asked

Viewed 630 times

2

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

How could you make sure the user doesn’t put absurd dates like:
11/20/2090 or 11/10/1890?

The only validation I do 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 as to whether the content is empty or not, it is possible?

There are no restrictions on the field being a String or date, I put String by practicality!

I have worked on some java web projects and doing this validation is something trivial on an 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 registered in the system and also see if that same date is greater than the highest date previously registered.
or better yet
the method takes three dates (Data, Data, Data, Data) and check if the Data is less than the Data or if the same date is greater than the Data.

public boolean validarData(LocalDate dataAtual, LocalDate dataRemota, LocalDate dataNascimento){
    //codigo que não sei ainda...
    return false;
}
  • 1
  • 1

    There are two different doubts in the same question, follow another link to the second doubt, but I advise to separate the second doubt in another question; http://www.botecodigital.info/java/a-api-de-data-do-java-8/

  • 1

    @Diegof No words old!! You gave me constructive tips!! This raises the morale of stackoverflow!!!

1 answer

2


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

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, 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, for dataNascimento is after the dataRemota, and prior to dataAtual. You can see both examples working here on IDEONE.

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


Addendum

If you receive a String and want to make the conversion to LocalDate, beyond the date itself, you need to do something similar as is done for the class Date, create a 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 );

How are you making use of textFields, will need to merge the above example before passing its date to the already exemplified method.

References:

How to compare Localdate instances Java 8

The JAVA 8 Date API

Localdate - Documentation

  • Thanks again Doctor Diego, formidable!! Shot and fall!

  • 1

    @Penapintada Dispo! I made an addendum in the answer, to show how to pass a String for LocalDate, since you are using text field, it would be interesting to know how to do.

  • Thank you very much, the addendum was enlightening, helped a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.