Compute age with Date()?

Hi,

I am doin a search application which I input an age from to:

example from 19 to 25.

In my database I have got the date of birth on this format: 24/08/1980

The question is how can I generate the age updated everytime with now, and how i can make the search from to ?

Thank you

[316 byte] By [Pierrick_WAa] at [2007-10-2 22:13:22]
# 1

> In my database I have got the date of birth on this

> format: 24/08/1980

If you do, then your database is poorly designed. If a date has a format, it's not a date, but a string. Your DB should be storing dates as dates, which do not have any format.

Assuming you fix you DB, you'd want to do something like this:

Create two Calendar objects, initialized to the current date.

In one, subtract 19 from the year field.

In the other, subtract 25 from the year field.

You now have one Calendar whose date is 19 years ago, and another whose date is 25 years ago. Somebody born between those dates will be betwen 19 and 25 today.

Get all entries from your DB where the D.O.B. is between those two dates.

If your DB is in fact an RDBMS that you're accessing through JDBC, you'll use a PreparedStatement's setDate() method.

[url=http://www.javaworld.com/jw-12-2000/jw-1229-dates.html]Calculating Java dates: Take the time to learn how to create and use dates[/url]

[url=http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html]Working in Java time: Learn the basics of calculating elapsed time in Java[/url]

[url=http://www.javaalmanac.com/egs/java.text/FormatDate.html]Formatting a Date Using a Custom Format[/url]

[url=http://www.javaalmanac.com/egs/java.text/ParseDate.html]Parsing a Date Using a Custom Format[/url]

http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html

jverda at 2007-7-14 1:30:12 > top of Java-index,Java Essentials,Java Programming...