Jump to content

calculate age

- - - - -

  • Please log in to reply
4 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
i want to calculate age throgh a java class but i have no idea how to that. can anybody please help me!

i want to do following things...

A.
input : Date of Birth - 13.08.1979 [dd/mm/yyyy]
output : age on Current Date (System Date) - 31 year(s) 7 month(s) 1 day(s)

B.
input : Date of birth - 13.08.1979 [dd/mm/yyyy]
input : in which date you want to calculate your age? - 13.08.2011 [dd/mm/yyyy]
output : your age on 13.08.2011 is : 32 year(s) 0 Month(s) 0 day(s)

#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
Post some code and we will help you.
But the full code will not be given on a silver platter...

The pseudo code will be something like:

Get the user input.
use a tokenizer to split the string into 3 parts.
get todays date.
todaysdate - userdate.
output what you calculate

#3
Nokturnal

Nokturnal

    Newbie

  • Members
  • Pip
  • 4 posts
@lintwurm

Could you give a quick explanation of what a tokenizer is?

Also, what would you do if you entered a date that was different than the current time?

#4
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
You don't even need to use a tokenizer... You can just use the .split() method to split the string into 3 parts.
for instance:
String[] bla = userInput.split("/");
As far as I remember

bla[0] will now be the dd part
bla[1] will be the mm part
and bla[2] will be the yyyy part.
You can use the systems date method to get the current age.
you can try this:
Date date = new Date();
// Keep the MM upper case
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = df.format(date);

That should give you the date in the format you want...
And that is if I remember correctly.

Hope that helps

#5
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
thanks to all




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users