how to calculate age on entry of dob in java

Solutions on MaxInterview for how to calculate age on entry of dob in java by the best coders in the world

showing results for - "how to calculate age on entry of dob in java"
Ismail
13 Aug 2019
1LocalDate today = LocalDate.now();
2LocalDate birthday = LocalDate.of(1987, 09, 24);
3
4Period period = Period.between(birthday, today);
5
6//Now access the values as below
7System.out.println(period.getDays());
8System.out.println(period.getMonths());
9System.out.println(period.getYears());
Leonardo
04 Aug 2019
1import java.text.ParseException;
2import java.text.SimpleDateFormat;
3import java.util.Calendar;
4import java.util.Date;
5 
6public class AgeCalculator
7{
8   private static Age calculateAge(Date birthDate)
9   {
10      int years = 0;
11      int months = 0;
12      int days = 0;
13 
14      //create calendar object for birth day
15      Calendar birthDay = Calendar.getInstance();
16      birthDay.setTimeInMillis(birthDate.getTime());
17 
18      //create calendar object for current day
19      long currentTime = System.currentTimeMillis();
20      Calendar now = Calendar.getInstance();
21      now.setTimeInMillis(currentTime);
22 
23      //Get difference between years
24      years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
25      int currMonth = now.get(Calendar.MONTH) + 1;
26      int birthMonth = birthDay.get(Calendar.MONTH) + 1;
27 
28      //Get difference between months
29      months = currMonth - birthMonth;
30 
31      //if month difference is in negative then reduce years by one 
32      //and calculate the number of months.
33      if (months < 0)
34      {
35         years--;
36         months = 12 - birthMonth + currMonth;
37         if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
38            months--;
39      } else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
40      {
41         years--;
42         months = 11;
43      }
44 
45      //Calculate the days
46      if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))
47         days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);
48      else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
49      {
50         int today = now.get(Calendar.DAY_OF_MONTH);
51         now.add(Calendar.MONTH, -1);
52         days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;
53      } 
54      else
55      {
56         days = 0;
57         if (months == 12)
58         {
59            years++;
60            months = 0;
61         }
62      }
63      //Create new Age object 
64      return new Age(days, months, years);
65   }
66 
67   public static void main(String[] args) throws ParseException
68   {
69      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
70      Date birthDate = sdf.parse("29/11/1981");     
71      Age age = calculateAge(birthDate);
72      System.out.println(age);
73   }
74}
75
queries leading to this page
get birth date in javajava calculate age in daysjava class get agejava get age from date of birthhow to find my age in javajava 8 get agehow to calculate average age in javacalculate age from date of birth string javacalculate age with localdate javaswitch date birthday to age javadate and date of birth calculate age javacalculate age based on date in javahow to display date of birth in javahow to calculate age on entry of dob in javajava code to calculate age from date of birthhow to calculate age from date of birth in javabirth year javabirthday in java datecalculate age in min in javajava program that works out age using current year and birthyearjava date of birth examplecount years from date of birth in javahow to calculate average age javajava date min value from agecalculate age using date of birth in javacalculate age from iiso date in javacalculate age year year month from dob local datejava check agecalculate age localdate javacalculate age using javajava calculate age from birthdayjava get agecalculate age in time jodaconvert dob into age in javacalculate the age from date of birth javadate of birth javajava calculate age from stringjava 11 calculate age from dobcalulate my age java how to get someone date of birth javacalculate age after time in javaget age from localdate javahow to find age from date of birth java 5cjava find age from date of birthcalculate age from date of birth in javajava calculate age with yearjava get age from datecheck is age of person greater than 18 in java how to calculate a date of birth base in year in javajava calculate agecalculate age in days javaage calculator in javajava calendar agejava date calculation agefunction to calculate age javacalculate age from dob in javajava count age from dateget age from java util dateget age by date of birth java 8how to calculate the age from the date of birth in javajava how to get age from date of birthjava birthdate datebirth date in javacalculate age from date birth javadate of birth and brithday in javajava how to calculate age by date of birthcalculate age from a date in string in javajava 8 localdatetime date of birthget age by date of birth javacalculate date of birth javahow to convert date of birth into age javacount year from my birth date in javawrite a java program to calculate the age from given date of birthhow to calculate age in java using current dateprogram to calculate age from date of birth in javacode for calculating the age from the date of birth is given java program calculate age javafind age using date of birth in javacalculate age from birth year javahow to calculate age from data of birth javajava age calculator date birthjava how to save date and calculate agecalculate date of birth when age is known javaget age from date in javafind age in javajava program to calculate age using date of birthprogram to find age from date of birth in javaget age from dob javajava age calendar virthhow to calculate the age by date of birth from a string javacalculate age program in javawrite a java program to calculate your age java calculate age in yearsjava program to calculate age from date of birthhow to calculate someone 27s age from date of birth javacalculate age from date javajava age from birthdatehow to calculate age from curent date and birth date in javacalculate age from date of birth in javaswhat year was i born calculator code in javacalculate age from date of birth javahow to work out age from date of birth javajava calendar get agehow to calculate the age by date of birth javajava born yearhow to compare age in javaget age from date javacalculate year of birth in javahow to calculate age from date of birth javajava calculate age with date formatwrite a java program to calculate your ageage calculation javaget age with java datecalculate age in javacheck age using date of birth javadate age javajava 8 localdate calculate agedate of birth in javacompute age from date javacalculate age using util date javacalculating current age javacalculate age from date in javacalculate age in java by birthdateage calculator javaget age from date of birth javajava calculate age from localdatejava create a dob programage calculation in spring boot java age from datejava 8 calculate age from date of birthjava function to calculate age from date of birthage from date of birth in javacalculate age from string with birth date javajava spring boot age calculator by birthdatehow to calculate age from date javahow to calculate ahe from date of birth javacalculate age from date of birth java examplesimple way of calculating age in javahow to confirm someone 27s age javajava calculate age from date of birthget latest birthday in stream 28 29 javafind birthday using periodbirthday calculator javahow to get age from date of birth in javalocaldate calculate agehow to have a maximum of age from birth date javahow many minutes your age in javajava spring boot api call age calculator by birthdatedate birth date javawrite a program that will ask the user e2 80 99s age and then tell them what year they were born in javaage calculation in javaread date of birth in javaage in days finder in java calculate age based on date of birth in java java age calculatorcalculate age with java by birth dateage calculation in java using date of birthcalculate age from timestamp javacaculate age from date of birth java datedate of birth from age javacalculating age in javahow to calcualate age in java given birtdatecalculate age java dateget car age javahow to calculate age in java how to write when you are born in java with the cal classjava change agecalculate date of birth from age in javahow to get age using birthday in javajava calculate age from birthyearreturn the age from current date of birth in terms of months in javacalculate age based on date of birth javacalculate age in java with java util datestring util age javahow to get an age from a date javacreate a class named agechecker 2 read in an integer value for the users age from the consolefind age from date of birth in javajava how oldjava calculate age in yearage using date of birth in javahow to calculate birthday javacheck the age using javaage calculate in javahow to compute age using birthday in javaage from birth date in javahow calculate age from a birthdate javafind your age in javavalidate age javahow to update age according to current date javahow to calculate age from data format javajava period birth ageformula to calculate the year of borth in javaget birthday from date javaget the age of someone by date of birth in javacurrent age calculator month day year in javalocaldate get agejava agesimple age calculator in javafunction to calcul age javabirthday calculate in java codeget age of date object javahow to make date birthday 3b object javahow to calculate age on entry of dob in java