How to calculate age by using birthday? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to calculate age by using birthday?

26th Mar 2017, 12:28 PM
Yasindra Himasha
Yasindra Himasha - avatar
3 Answers
+ 11
@Raphael This won't work for birthdays that will be later in the current year than current date. Use Date class, you can find a tutorial there: https://www.tutorialspoint.com/java/java_date_time.htm
26th Mar 2017, 12:59 PM
Tashi N
Tashi N - avatar
+ 3
You just need to do subtraction of the actual year by birthday: Java example: public class HowOld { public static void main (String[] args) { int birthday = 2000; int actualYear = 2016; int old; old = actualYear - birthday; // (2016 - 2000 = 16 years) System.out.println(old); // Shows: 16. } }
26th Mar 2017, 12:48 PM
Raphael Coelho
Raphael Coelho - avatar
+ 3
@Tashi Thanks! I dont nave think this possibilite! :)
26th Mar 2017, 3:15 PM
Raphael Coelho
Raphael Coelho - avatar