Friday, 20 January 2012

How to calculate an age in Java?

Can we calculate an age in java without worrying about number of leap years between birth date and current date? Is there simple way to calculate it? …. The answer is yes. When we calculate somebody’s age manually, we do not think about number of leap year or number of days in each year. Same logic can be applies for calculating age programmatically.

Following is a code snippet for calculating the age. Please leave comment if it has helped you. Also please let me know if you feel this can be further optimized or this can be achieved by more simpler way...

  int years = 0;
  int months = 0;
  int days = 0;
  
  //create calendar object for birth day
  Calendar birthDay = Calendar.getInstance();
  birthDay.set(Calendar.YEAR, 2005);
  birthDay.set(Calendar.MONTH, Calendar.SEPTEMBER);
  birthDay.set(Calendar.DATE, 29);
  
  //create calendar object for current day
  long currentTime = System.currentTimeMillis();
  Calendar currentDay = Calendar.getInstance();
  currentDay.setTimeInMillis(currentTime);

  //Get difference between years
  years = currentDay.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);

  
  int currMonth = currentDay.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(currentDay.get(Calendar.DATE)<birthDay.get(Calendar.DATE))
    months--;
   
  }else if(months == 0 && currentDay.get(Calendar.DATE) < birthDay.get(Calendar.DATE)){
   years--;
   months = 11;
  }
  
  
  //Calculate the days
  if(currentDay.get(Calendar.DATE)>birthDay.get(Calendar.DATE))
   days = currentDay.get(Calendar.DATE) -  birthDay.get(Calendar.DATE);
  else if(currentDay.get(Calendar.DATE)<birthDay.get(Calendar.DATE)){
   int today = currentDay.get(Calendar.DAY_OF_MONTH); 
   currentDay.add(Calendar.MONTH, -1);
   days = currentDay.getActualMaximum(Calendar.DAY_OF_MONTH)-birthDay.get(Calendar.DAY_OF_MONTH)+today;
  }else{
   days=0;
   
   if(months == 12){
    years++;
    months = 0;
   }
  }
  
  System.out.println("The age is : "+years+" years, "+months+" months and "+days+" days" );


15 comments:

  1. I have checked it with many dates and it is correct on every occasion.Good work.

    ReplyDelete
  2. Hi,

    find Age Calculator then it is Overall looking for a Chronological age or anniversary calculator then this is your best option in age Calculator.

    Age Calculator By Date Of Birth (Days, Months)
    Calculation of age with interesting details!
    If you want to know the details of your age, then install the Age Calculator

    ReplyDelete
  3. Find your age with the assistance of particular handy age calculator. You can readily calculate age in years, months, days, weeks, hours, and minutes together with the assistance of the birthday calendar.

    ReplyDelete
  4. Thanks for sharing awesome information with us.

    i started my journey as a data scientist, faced many difficulies while searching for Job.Things are hardly any different in India than the rest of the world. Experience speaks the last word in the corporates. But in cities like Bangalore you can get a job or an internship at a startup quite easily. You need to be trained first. Data science training in Bangalore is easily available and top notch.

    ReplyDelete
  5. Very impressive content with new updates.Thanks for the post.Keep updating more.If you are looking for Hadoop Admin course then visit Hadoop Admin Training Institute

    ReplyDelete
  6. Awesome and informative data,
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    Please visit our site,
    buylogo

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. You're doing a great job Man,Keep it up. Love your site. My thanks for doing such a good job. I technical skills to learn will come back to read more and inform my coworkers about your site.

    ReplyDelete
  10. Keep sharing continue with us.
    It's very unique and helpful for us.
    Custom Logo

    ReplyDelete
  11. Hi, it's me mir I really was worried about my design & fashion business but my friend told me to search for some kind of design articles & I did thanks for sharing this helpful content I really appreciate it,
    Hey, if you want to grow your business by web designing or logo designing then visit us?
    Logo Designers

    ReplyDelete
  12. Step into the future with Explore Data Integration with Azure Data Factory Training in Hyderabad. Boost your skills and secure top IT roles with our expert-led courses

    ReplyDelete