First of all, the use of classes like java.util.Date
and java.util.Calendar
should be avoided, being alternative library classes Joda-Time or classes as LocalDate
and LocalDateTime
, in addition to the rest of the date and time API java.time
present from Java 8.
Using the Java 8 API mentioned above, we can use the class Period
to calculate the time interval between two dates, including the number of years. Thus, a possible implementation of a routine would be as follows:
public static int idade(final LocalDate aniversario) {
final LocalDate dataAtual = LocalDate.now();
final Period periodo = Period.between(aniversario, dataAtual);
return periodo.getYears();
}
The date is like LocalDate
, object that contains date only, does not contain time and does not display class time zone problems java.util.Date
.
The method Period#between
calculates the period between the birthday date and the current date created with LocalDate#now
.
Finally, the routine returns the number of years of the period.
After understanding how the method works, we could abbreviate it without the auxiliary variables as follows:
public static int idade(final LocalDate aniversario) {
return Period.between(aniversario, LocalDate.now()).getYears();
}
If, for some reason, the program needs to use different variables for day, month and year, just apply LocalDate.of
to create a date from the values and reuse the above routine:
public static int idade(final int dia, final int mes, final int ano) {
return idade(LocalDate.of(ano, mes, dia));
}
If, for another reason, the program needs to use dates of the type java.util.Date
, we can perform the conversion to type LocalDate
and again reuse the above routine:
public static int idade(final Date dataAniversario) {
return idade(LocalDateTime.ofInstant(dataAniversario.toInstant(), ZoneOffset.UTC).toLocalDate());
}
Once again I remember that the insistence on using java.util.Date
, java.util.Calendar
and associated classes such as SimpleDateFormat
and DateFormat
often lead to problems in date manipulation and calculation due to time zone problems. This means that in periods affected by daylight saving time, for at least one hour, birthdays have the wrong value. out other more serious problems when adding and subtracting time.
go wrong in what sense? which errors are presented?
– Paulo Roberto
Wrong because the displayed age is incorrect, for example.
14/06/1992
= 22 years. The result of this method is 23, would be 23 if the date of birth was greater than or equal to15/06/1992
– DiegoAugusto
This is not directly related to your question, but I would change the function signature to
getIdade(java.util.Date dataNasc, java.util.Date hoje = null)
andif (hoje == null) { Calendar today = Calendar.getInstance(); } else { Calendar today = new GregorianCalendar(); today.setTime(hoje); }
: this way, it is possible to write automatic tests for your function (you would spend a day "today" fixed to your function in the tests).– user25930
Behold that answer. The class Chronounit also supports
YEARS
andMONTHS
. Finally, if you want to compute all three at once use the methodPeriod.between
.– Anthony Accioly
Calculation of age in years, months and days:<br> http://forum.imasters.com.br/topic/552949-idade-em-javafx-em-anos-meses-e-days/
– user56442