TechArena Community Calculating Age in java
 User Name Remember Me? Password

#1
03-02-2010
 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
03-02-2010
 Member Join Date: Nov 2009 Posts: 446
Re: Calculating Age in java

Hello,
For the current date in milliseconds:
Quote:
 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
03-02-2010
 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
03-02-2010
 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);
cu.add(Calendar.YEAR,-yeardiff);
if(brt.after(cu))
{
yeardiff = yeardiff -- 1;
}
return yeardiff;
}```
#5
03-02-2010
 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
03-02-2010
 Member Join Date: Nov 2009 Posts: 359
Re: Calculating Age in java

Hello,
Please see the following code, I hope it will help you
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);
cu.add(Calendar.YEAR,-yeardiff);
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));
}
}```

 Tags:

 Thread Tools Search this Thread Search this Thread: Advanced Search

 Similar Threads for: "Calculating Age in java" Thread Thread Starter Forum Replies Last Post Need help calculating Age in FileMaker Pro NathanDS Windows Software 4 29-05-2010 05:16 PM Problem calculating duration in java Gunner 1 Software Development 5 05-02-2010 04:56 AM Calculating the sum of two polynomials Liquid Shadow Software Development 3 30-11-2009 12:12 PM Calculating PC Power Consumption Justvicks Hardware Peripherals 4 01-12-2008 02:19 PM Calculating frequency in RPM with JAVA gvBlake Software Development 2 23-08-2008 06:02 PM

All times are GMT +5.5. The time now is 10:42 AM.

 Contact Us - TechArena - Privacy Statement - Terms of Service - SiteMap