Bienvenido a nuestro sitio web, aquí vas a encontrar la solucíon que estabas buscando.
Ejemplo 1: java age from date
LocalDate today =LocalDate.now();LocalDate birthday =LocalDate.of(1987,09,24);Period period =Period.between(birthday, today);//Now access the values as belowSystem.out.println(period.getDays());System.out.println(period.getMonths());System.out.println(period.getYears());
Ejemplo 2: como calcular la edad en la entrada de dob en java
importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;publicclassAgeCalculatorprivatestaticAgecalculateAge(Date birthDate)int years =0;int months =0;int days =0;//create calendar object for birth dayCalendar birthDay =Calendar.getInstance();
birthDay.setTimeInMillis(birthDate.getTime());//create calendar object for current daylong currentTime =System.currentTimeMillis();Calendar now =Calendar.getInstance();
now.setTimeInMillis(currentTime);//Get difference between years
years = now.get(Calendar.YEAR)- birthDay.get(Calendar.YEAR);int currMonth = now.get(Calendar.MONTH)+1;int birthMonth = birthDay.get(Calendar.MONTH)+1;//Get difference between months
months = currMonth - birthMonth;//if month difference is in negative then reduce years by one //and calculate the number of months.if(months <0)
years--;
months =12- birthMonth + currMonth;if(now.get(Calendar.DATE)< birthDay.get(Calendar.DATE))
months--;elseif(months ==0&& now.get(Calendar.DATE)< birthDay.get(Calendar.DATE))
years--;
months =11;//Calculate the daysif(now.get(Calendar.DATE)> birthDay.get(Calendar.DATE))
days = now.get(Calendar.DATE)- birthDay.get(Calendar.DATE);elseif(now.get(Calendar.DATE)< birthDay.get(Calendar.DATE))int today = now.get(Calendar.DAY_OF_MONTH);
now.add(Calendar.MONTH,-1);
days = now.getActualMaximum(Calendar.DAY_OF_MONTH)- birthDay.get(Calendar.DAY_OF_MONTH)+ today;else
days =0;if(months ==12)
years++;
months =0;//Create new Age object returnnewAge(days, months, years);publicstaticvoidmain(String[] args)throwsParseExceptionSimpleDateFormat sdf =newSimpleDateFormat("dd/MM/yyyy");Date birthDate = sdf.parse("29/11/1981");Age age =calculateAge(birthDate);System.out.println(age);
Aquí tienes las reseñas y calificaciones
Si te ha resultado de utilidad nuestro post, agradeceríamos que lo compartas con más programadores y nos ayudes a extender este contenido.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)