# Thread: Calculating Age in java

1. Member
Join Date
Dec 2009
Posts
292

## Calculating Age in java

Hello,
How can I retrieve the system date in java and calculate the age of a person knowing his date of birth? If possible, give me the full syntax. Any idea regarding this topic will be highly appreciated. Thank you in advance

2. Member
Join Date
Nov 2009
Posts
446

## Re: Calculating Age in java

Hello,
For the current date in milliseconds:
long test = System.currentTimeMillis();
to the date of birth:
Code:
`java.util.Calendar Brh = Calendar.getInstance();`
Then you have to play with the setters, and it retrieves the date in milliseconds as follows:
Code:
`long test1 = birth.getTime();`
Subtraction will do for the rest and I'll leave it to the conversion years.

3. Member
Join Date
Nov 2009
Posts
359

## Re: Calculating Age in java

Hello,
You can extract the years, get (Calendar.YEAR). You will get and the year of birth and the current year, example
Code:
```February 13, 1985 -> 1985
October 22, 2009 -> 2009```
In this case the person in 24 years, then uses the method add (Calendar.YEAR, -24), and compares the date, removes one years if 24 years before the person is not born: she was born after October 22.

4. Member
Join Date
Nov 2009
Posts
583

## Re: Calculating Age in java

Hello,
For the above explanation, I have developed a code, please check it out. Hope this will help you.
Code:
```Public static int getYear(Date of)
{
Calendar cu = Calendar.getInstance();
Calendar brt = Calendar.getInstance();
brt.setTime(d);
int yeardiff = cu.get(Calendar.YEAR) - Birth.get(Calendar.YEAR);
if(brt.after(cu))
{
yeardiff = yeardiff -- 1;
}
return yeardiff;
}```

5. Member
Join Date
Nov 2009
Posts
518

## Re: Calculating Age in java

Hello,
In the calendar class there are set methods that allow all precisely define what we want:
Code:
`set(int year, int month, int date)`
So you create an object Calendar Using the method static Calendar getInstance ()Then you change that date at its convenience. When you look in the javadoc, which function returns a date: you need a date. You should not even need to read the javadoc for this finding: it is in my program, but still Surprise is getTime (). So after long search in the page cited above we find the function getTime (), so you get the Date object.

6. Member
Join Date
Nov 2009
Posts
359

## Re: Calculating Age in java

Hello,
Code:
```import java.util.Calendar;
import java.util.Date;
class Test2
{
Public static int getYear(Date of)
{
Calendar cu = Calendar.getInstance();
Calendar brt = Calendar.getInstance();
brt.setTime(d);
int yeardiff = cu.get(Calendar.YEAR) - Birth.get(Calendar.YEAR);
if(brt.after(cu))
{
yeardiff = yeardiff -- 1;
}
return yeardiff;
}

Public static void hand(String[] args)
{
if(args.length != 3) { System.err.System.out.println("Standard"); System.exit(1); }
Calendar ca2 = Calendar.getInstance();
ca2.set(Integer.parseInt(args[0]), Integer.parseInt(args[1])-1, Integer.parseInt(args[2]));
Date bday = ca2.getTime();
System.out.System.out.println("The age of the master:"+ getYear(bday));
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•