This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes DateFormat and Calculation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "DateFormat and Calculation" Watch "DateFormat and Calculation" New topic
Author

DateFormat and Calculation

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 165

Hi all,
I have a textfield, in which the user enters his day of birth (in dd/mm/yy format). I would like to calculate the age of user when he enters this.
1. getCurrentDate
2. format it in order to be same with the textfield
3. substract

Is it right? or what is the best way to do this?
Thanks for any help
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
create 2 calendar objects
dob (set to textfield date)
now (set to current time)
int age = now.get(Calendar.YEAR) - dob.get(Calendar.YEAR)
add age to dob
if(dob.after(now)) age --; //hasn't had birthday yet
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
How does that work out for somebody born last week?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> How does that work out for somebody born last week?

it would return 0
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: DateFormat and Calculation
 
Similar Threads
Formatting of textfield
problem with showMessageDialog
storing strings in a specific format
placing \" in the textfield causes error 500
Number of charecters in JTextField