how to calculate age on entry of dob in java

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1987, 09, 24);

Period period = Period.between(birthday, today);

//Now access the values as below
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears());

4.38
8
Phoenix Logan 186120 points

                                    import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class AgeCalculator
{
   private static Age calculateAge(Date birthDate)
   {
      int years = 0;
      int months = 0;
      int days = 0;
 
      //create calendar object for birth day
      Calendar birthDay = Calendar.getInstance();
      birthDay.setTimeInMillis(birthDate.getTime());
 
      //create calendar object for current day
      long 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--;
      } else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
      {
         years--;
         months = 11;
      }
 
      //Calculate the days
      if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))
         days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);
      else if (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 
      return new Age(days, months, years);
   }
 
   public static void main(String[] args) throws ParseException
   {
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      Date birthDate = sdf.parse("29/11/1981");     
      Age age = calculateAge(birthDate);
      System.out.println(age);
   }
}

4.38 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
age using date of birth in java get birth date in java calculate age in days java java calculate age in days find age using date of birth in java calculate age using util date java get age by date of birth java 8 get age by date of birth java get the age of someone by date of birth in java date of birth java calculate age based on date of birth java how to convert date of birth into age java calculate age from string with birth date java java date calculation age java calculate age in year java calculate age in years java calculate age check the age using java find age in java how to calculate age in java using current date get age of date object java java program to calculate age using date of birth java how to save date and calculate age calculate age java how to get an age from a date java date age java java get age how to calculate age from data of birth java calculate age from date birth java how to calculate the age by date of birth java calculate age from a date in string in java how to calculate age from date java how to calculate age from data format java date birth date java calculate the age from date of birth java java get age from date of birth java birthdate date program to find age from date of birth in java java age calculator date birth calculate date of birth from age in java get age from localdate java java find age from date of birth calculate age from birth year java how to calculate age from date of birth java birth date in java calculate age from date of birth in javas calculate age from date of birth java calculate age with java by birth date java how to calculate age by date of birth calculate age in java how to calculate the age from the date of birth in java calculating age in java how to calculate age from curent date and birth date in java calculating current age java java function to calculate age from date of birth how to update age according to current date java date of birth in java how calculate age from a birthdate java java 8 get age how to work out age from date of birth java date of birth from age java calculate age java date age calculate in java age from birth date in java birthday in java Date how to calculate someone's age from date of birth java calculate age in min in java how many minutes your age in java compute age from Date java how to find my age in java java create a DOB program java calculate age from localdate how to make Date birthday; object java java date of birth example how to get someone date of birth java java how old java spring boot api call age calculator by birthdate java spring boot age calculator by birthdate calculate age in time joda calculate age using java Write a Java program to calculate your age. java change age write a java program to calculate your age localdate calculate age Write a program that will ask the user’s age and then tell them what year they were born in. java age calculator in java Create a class named AgeChecker 2. Read in an integer value for the users age from the console get latest birthday in stream() java java code to calculate age from date of birth how to calculate age from date of birth in java switch date birthday to age java date of birth and brithday in java get age from dob java how to find age from date of birth java\ get age from date of birth java what year was i born calculator code in java formula to calculate the year of borth in java calculate age after time in java calculate age based on date in java java class get age calculate age program in java age calculation in java using date of birth birthday calculator java calculate age from timestamp java convert dob into age in java java calculate age from birthyear java calculate age from birthday age calculation in java java period birth age java age calendar virth java calendar age calculate age from dob in java birth year java java calculate age from date of birth date and date of birth calculate age java how to compare age in java check is age of person greater than 18 in java age calculation in spring boot calculate age based on date of birth in java find birthday using period how to calculate birthday java java 8 localdatetime date of birth java 8 calculate age from date of birth get car age java LocalDate get age find age from date of birth in java calculate age from date of birth string java birthday calculate in java code java calculate age from string age calculation java java age validate age java java program that works out age using current year and birthyear how to write when you are born in java with the cal class calculate age in java by birthdate how to calculate age in java java program to calculate age from date of birth java born year calculate age year year month from dob local date calculate age from date of birth in java age from date of birth in java calculate age with localdate java how to confirm someone's age java java 11 calculate age from dob get age from date in java get age with java date java how to get age from date of birth calculate age from date in java java 8 localdate calculate age java get age from date calculate age using date of birth in java function to calculate age java function to calcul age java string util age java get birthday from date java caculate age from date of birth java Date how to calculate age on entry of dob in java how to get age from date of birth in java calulate my age java java age from birthdate java age from date
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source