Date calculation

HelloI want to calculate age of a person by comparing his/her birthday (say 1977-08-18) with todays date (say 2007-05-07) so that i can determined whether he is 18 above or not..... what should i do?, please guide me..Thank you
[255 byte] By [Java-Manoja] at [2007-11-27 3:33:55]
# 1
Check out the GregorianCalendar class, with it you can do date calculations. http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html
gimbal2a at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Don't offer dukes when you never give them out
robtafta at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Date d1 = person1.getDob();

long agediff = d1.getTime() - new Date().getTime();

agediff would give you the difference in number of milliseconds between two dates.

you may put a factor like 18yrs = 568036800000milli seconds.

and compare agediff > 568036800000 or not.

RahulSharnaa at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> Date d1 = person1.getDob();

> long agediff = d1.getTime() - new

> Date().getTime();

>

> agediff would give you the difference in number of

> milliseconds between two dates.

>

> you may put a factor like 18yrs = 568036800000milli

> seconds.

>

> and compare agediff > 568036800000 or not.

That doesn't work. Leap years aren't taken into account.

A difference in dates cannot be measured in milliseconds.

es5f2000a at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thank you Rahul,Its of great help...But how did you calculate the 18 years factor, Now i need to know the factor for 14 years, please tell me..
Java-Manoja at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

if taken account od leap year im considering 365.25 days per year.

therefore

(18) X (365.25) X (24) X (3600) X 1000 = (18) X 31557600000 = 568036800000 milli seconds

therefore if you are calculating it for 14yrs the factor has to be

(14) X (365.25) X (24) X (3600) X 1000 = (14) X 31557600000 = 441806400000 milli seconds

hope that makes sense...

if that did help you don't forget to assign duke stars which you promised.

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

> if taken account od leap year im considering 365.25

> days per year.

> therefore

>

> (18) X (365.25) X (24) X (3600) X 1000 = (18) X

> 31557600000 = 568036800000 milli seconds

>

> therefore if you are calculating it for 14yrs the

> factor has to be

>

> (14) X (365.25) X (24) X (3600) X 1000 = (14) X

> 31557600000 = 441806400000 milli seconds

Which is also wrong. Each year does not contain .25 of a leap day.

A year contains either 0 or 1 leap days. Using Calendar is

correct. Trying to use milliseconds is incorrect.

es5f2000a at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
dude r u using any databses? they provide pretty n easier date and time functions. execute them n try.... if u dont have any idea tell me..... i use mysql n i have used mysql's built in functions. hope u this helps u....
kiranbedrea at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Calculating with hardcoded millis? Amazing ..

Well, generally you should use Calendar to calculate the date difference. Sun Java core API doesn't provide kind of default "elapsedYears" functionality, so that you have to write it yourself. Generally it's just adding the desired difference in a while (olderCal.before(newerCal)) loop and keeping a counter inside.

Example:Calendar birthdate = Calendar.getInstance();

birthdate.clear();

birthdate.set(1977, 7, 18); // Month starts with 0.

Calendar today = Calendar.getInstance();

int elapsedYears = -1;

while (birthdate.before(today)) {

birthdate.add(Calendar.YEAR, 1);

elapsedYears++;

}

System.out.println(elapsedYears);

BalusCa at 2007-7-12 8:37:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...