Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

calculate age

Thread view: 
esokol@cerner.com - 31 Jan 2007 16:19 GMT
Is there an easy way to calculate number of years using standard
Java?  I have seen some examples but they use a product using
BigDate.  I need something that will calculate number of years taking
into account leap years.

Thanks
Elly
Eric Sosman - 31 Jan 2007 17:11 GMT
esokol@cerner.com wrote On 01/31/07 11:19,:
> Is there an easy way to calculate number of years using standard
> Java?  I have seen some examples but they use a product using
> BigDate.  I need something that will calculate number of years taking
> into account leap years.

   Could you give an example of exactly what you mean by
"calculate number of years?"

Signature

Eric.Sosman@sun.com

Arne Vajhøj - 01 Feb 2007 01:04 GMT
> esokol@cerner.com wrote On 01/31/07 11:19,:
>> Is there an easy way to calculate number of years using standard
[quoted text clipped - 4 lines]
>     Could you give an example of exactly what you mean by
> "calculate number of years?"

The subject line gives some hints to what it could mean.

Arne
Arne Vajhøj - 01 Feb 2007 01:03 GMT
> Is there an easy way to calculate number of years using standard
> Java?  I have seen some examples but they use a product using
> BigDate.  I need something that will calculate number of years taking
> into account leap years.

From some old code:

   private static int age(int y, int m, int d) {
      Calendar cal = new GregorianCalendar(y, m, d);
      Calendar now = new GregorianCalendar();
      int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
      if((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH)) ||
         (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) &&
         cal.get(Calendar.DAY_OF_MONTH) >
now.get(Calendar.DAY_OF_MONTH))) {
         res--;
      }
      return res;
   }

changing from y m d to Date is left as an exercise.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.