• Post Reply Bookmark Topic Watch Topic
  • New Topic

DateFormat and Calculation  RSS feed

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does that work out for somebody born last week?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> How does that work out for somebody born last week?

it would return 0
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!