Aaron Gadberry

Help – v. helped, help·ing, helps

Was this site helpful?
My Amazon.com Wishlist

Archive for August, 2007

Find the Difference Between two Java Dates (Calendars)

17th August 2007 - By Aaron Gadberry

Well the first question to ask is what unit do you want the difference in. This is one of the reasons many people stick to incremental date math instead of math returning an int.

Lets say you want to do a basic age calculation. How old is Joe? His birthday is 7/29/1981. Well you could subtract 1981 from today, then determine if he had a birthday this year, which could mean comparing everything up to the milliseconds. The problem is, such a calculation is not as accurate as it would seem. This style method fails to preserve the validity of the passing of leap years, leap seconds, etc. If you asked how many days old Joe is then your answer would be something like 365 * (current – 1981) + number of days this year – number of days between Jan 1 and July 29. Well you forgot a few leap days in there, at least 5.
Read the rest of this entry »

Posted in Computers, Programming | 4 Comments »