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...
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" );
I have checked it with many dates and it is correct on every occasion.Good work.
ReplyDeleteHi,
ReplyDeletefind 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
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.
ReplyDeleteThanks for sharing awesome information with us.
ReplyDeletei 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.
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
ReplyDeleteAwesome and informative data,
ReplyDeleteThanks for sharing with us,
We are again come on your website,
Thanks and good day,
Please visit our site,
buylogo
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYou'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.
ReplyDeleteKeep sharing continue with us.
ReplyDeleteIt's very unique and helpful for us.
Custom Logo
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,
ReplyDeleteHey, if you want to grow your business by web designing or logo designing then visit us?
Logo Designers
fon perde modelleri
ReplyDeletesms onay
Türk telekom mobil ödeme bozdurma
nftnasilalinir.com
Ankara Evden Eve Nakliyat
TRAFİK SİGORTASI
DEDEKTÖR
Website Kurma
Ask Romanlari
smm panel
ReplyDeletesmm panel
iş ilanları
instagram takipçi satın al
HİRDAVATCİ
Www.beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi
I appreciate you sharing this fantastic article.
ReplyDeleteazure data factory training in Hyderabad
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