Calculate age

From CodeCodex

Implementations[edit]

Java[edit]

 import java.util.GregorianCalendar;
 import java.util.Calendar;
 
 public class CalcAge {
 
   public static void main(String [] args) {
     // remember ... months are 0-based : jan=0 feb=1 ...
     System.out.println
       ("1962-11-11 : " + age(1962,10,11));
     System.out.println
       ("1999-12-03 : " + age(1999,11,3));
   }
 
   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;
   }
} 

Ruby[edit]

In Date of Ruby, the month begins with 1.

require 'date'
def calc_age(year, mon, day)
  cal = Date.new(year, mon, day)
  now = Date.today
  res = now.year - cal.year
  res -= 1  if cal.mon > now.mon || (cal.mon == now.mon && cal.mday > now.mday)
  res
end

dates = [[1962, 11, 11], [1999, 12, 3]]
dates.each do |y,m,d|
  printf "%4d-%02d-%02d : %2d\n", y, m, d, calc_age(y,m,d)
end