Check if a date is before or after other dates

Asked

Viewed 278 times

2

I am using Java 7 and need to check if a date of birth is before or after certain dates.

I’m doing like this:

String  str1 ="31/12/1960";  
Date date1 = new SimpleDateFormat("dd/MM/yyyy").parse(str1);  

String  str2 ="31/12/2003";  
Date date2 = new SimpleDateFormat("dd/MM/yyyy").parse(str2); 

if (individuo.getDataNascimento() < date1 && nascimento > date2); {
addmsg ("motivo de isenção nao condiz com a idade");

}
else {
prontuario.setDataNascimento(individuo.getDataNascimento());
            log.info("Data Nascimento Indivíduo: " + prontuario.getDataNascimentoFormatado());

}

The problem is that the symbols of < and > are not recognized and it does not compile.

1 answer

2

To compare whether one date comes before or after another, use the methods before and after:

if (individuo.getDataNascimento().before(date1) || individuo.getDataNascimento().after(date2)) {
   etc...
}

One detail is that I actually think you should use the operator || ("or") instead of && ("and"). For the && checks whether both conditions are true, and there is no way that a date can be at the same time before 1960 and after 2003 (either one or the other, both at the same time do not give, so if you use && will never get into this if).


Another detail is that you don’t need to create a new SimpleDateFormat all the time. If the format of the strings does not change, just reuse the same:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

String str1 ="31/12/1960";
Date date1 = sdf.parse(str1);

String str2 ="31/12/2003";
Date date2 = sdf.parse(str2);

An alternative to Java 7 - if you want to use an external library - is Threeten Backport, one backport of java.time (the new Java 8 Date API).

With this, you can use a more suitable type for each situation. For example, as the date of birth only has day, month and year instead of Date you can use a org.threeten.bp.LocalDate:

public class SuaClasse {
    private LocalDate dataNascimento;

    public LocalDate getDataNascimento() {
        return dataNascimento;
    }

    // etc...
}


DateTimeFormatter parser = DateTimeFormatter.ofPattern("dd/MM/uuuu");
String str1 = "31/12/1960";
LocalDate inicio = LocalDate.parse(str1, parser);

String str2 = "31/12/2003";
LocalDate fim = LocalDate.parse(str2, parser);

if (individuo.getDataNascimento().isBefore(inicio) || individuo.getDataNascimento().isAfter(fim)) {
    // data está antes de "inicio" ou depois de "fim"
}

It may seem that "only changed the date class", but the new API has many advantages and improvements compared to Date, and its use from now on would facilitate a future migration to Java 8, since changing the name of the org.threeten.bp for java.time (save one or another specific detail, see more details here and here - in particular at the end of this answer has some information about the use of backport).

Browser other questions tagged

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