br Desk Top

Official Debian GNU/Linux package

  • Tech News
  • Linux
  • Windows
  • Android
  • Security
  • Mobile
  • Gaming

Java Calculate Age From Date of Birth

September 25, 2017 By Lenny

java codeKnow how many years, months and days you have been living in this world today.

Java programming is really interesting. Since I started as a student, computer programming has really become my interest!

So today I wanted to share a simple Java program that anyone can create themselves just to give you an idea of what Java is and what it can do. You don’t need to be a sick coder or anything, this is completely newbie friendly and I’ve given you all the code you’ll need to be able to do this yourself.

So let’s get started…

import java.util.Scanner;
import java.util.Calendar;

public class CalculateAge {

public static void main(String args[]){
Scanner input=new Scanner(System.in);

System.out.print (”Enter birth date [yyyy/mm/dd]: “);
String yourBirthDay=input.nextLine();

Calendar today=Calendar.getInstance(); //get the date today
String now=String.format(”%tF”,today); //format the date today into “yyyy/mm/dd” format

int yourDayAge=0;
Read more in Java
« Jboss Enterprise Application Platform 5.0, The Basis for Cloud Computing

//temporarily get the month part of your age

int yourMonthAge=getMonth(now) – getMonth(yourBirthDay);
int yourYearAge=getYear(now) – getYear(yourBirthDay); //temporarily get the year part of your age

//if the day part of your age is greater than the day part of the date today,
//subtract the yourMonthAge variable with 1 and add 30 days to the day part of the date today
if(getDay(now) < getDay(yourBirthDay))
{
yourMonthAge–;
yourDayAge=(getDay(now) + 30) – getDay(yourBirthDay);
}
else
{
yourDayAge=getDay(now) – getDay(yourBirthDay);
}

//if the month part of your age is greater than the month part of the date today,
//subtract the yourYearAge variable with 1 and add 12 months to the month part of the date today
if(getMonth(now) < getMonth(yourBirthDay)){
yourYearAge–;
yourMonthAge=(getMonth(now) + 12) – getMonth(yourBirthDay);
}
else
{
yourMonthAge=getMonth(now) – getMonth(yourBirthDay);
}

//Print the final result of the calculation
System.out.println (”You are ” + yourYearAge + ” year(s), ” + yourMonthAge + ” month(s), and ” + yourDayAge + ” day(s) old.”);
}//end of main

//gets the year part of the specified date function argument/parameter
public static int getYear(String date){
return Integer.parseInt(date.substring(0,4));
}//end of method getYear

//gets the month part of the specified date function argument/parameter
public static int getMonth(String date){
return Integer.parseInt(date.substring(5,7));
}end of method getMonth

//gets the day part of the specified date function argument/parameter
public static int getDay(String date){
return Integer.parseInt(date.substring(8,10));
}end of method getDay

}//end class CalculateAge

Note: Leap Year is not handled here but for you to enhance your knowledge, I did not include it here.

Hint: If the current year is divisible by 4, it means the current year is in a leap year.

So that’s it. I hope you like this bit of code. And if you want more, then just comment and let me know what you’re looking for.

Filed Under: Android

w3 org

What is BRDesktop?

BrDesktop

Site Secured

Logo: Shield Security
This Site Is Protected By
Shield Security →

DMCA.com Protection Status

BRDesktop.org is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com
Sitemap | Privacy Policy | TOS