469,270 Members | 1,108 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

calculating the age from current date

karthickkuchanur
156 100+
Hai sir,
i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically,
i refer to google it was confusing me .please give some idea to do sir
Feb 15 '08 #1
15 42084
hirak1984
316 100+
Hai sir,
i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically,
i refer to google it was confusing me .please give some idea to do sir
so what is the problem?
are you finding it difficult to calculate? or to write the code?
first learn the calculation.
tell us how would you calculate if the question was there in your mathematics class? post the formula.
Feb 15 '08 #2
r035198x
13,262 8TB
Hai sir,
i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically,
i refer to google it was confusing me .please give some idea to do sir
Have a look at java.util.GregorianCalendar, try some code, and post again if you still have problems.
Feb 15 '08 #3
karthickkuchanur
156 100+
Have a look at java.util.GregorianCalendar, try some code, and post again if you still have problems.
ok sir i will try tot do that
Feb 15 '08 #4
karthickkuchanur
156 100+
so what is the problem?
are you finding it difficult to calculate? or to write the code?
first learn the calculation.
tell us how would you calculate if the question was there in your mathematics class? post the formula.
to develope the code i have the set code in google but i cant able to study that
Feb 15 '08 #5
r035198x
13,262 8TB
to develope the code i have the set code in google but i cant able to study that
What do you mean by that?
Feb 15 '08 #6
karthickkuchanur
156 100+
What do you mean by that?
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class AgeCalculation{
  5.   public static void main(String[] args) throws IOException{
  6.     int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
  7.     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  8.     Calendar cd = Calendar.getInstance();
  9.     try{
  10.       System.out.print("Enter year of your date of birth : ");
  11.       year = Integer.parseInt(in.readLine());
  12.       if(year > cd.get(Calendar.YEAR)){
  13.         System.out.print("Invalid date of birth.");
  14.         System.exit(0);
  15.       }
  16.       System.out.print("Enter month of your date of birth : ");
  17.       month = Integer.parseInt(in.readLine());
  18.       if(month < 1 || month > 12){
  19.         System.out.print("Please enter monthe between 1 to 12.");
  20.         System.exit(0);
  21.       }
  22.       else{
  23.         month--;
  24.         if(year == cd.get(Calendar.YEAR)){
  25.           if(month > cd.get(Calendar.MONTH)){
  26.             System.out.print("Invalid month!");
  27.             System.exit(0);
  28.           }
  29.         }
  30.       }
  31.       System.out.print("Enter day of your date of birth : ");
  32.       day = Integer.parseInt(in.readLine());
  33.       if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || 
  34. month == 9 || month == 11){
  35.         if(day > 31 || day < 1){
  36.           System.out.print("Please enter day between 1 to 31.");
  37.           System.exit(0);
  38.         }
  39.       }
  40.       else if(month == 3 || month == 5 || month == 8 || month == 10){
  41.         if(day > 30 || day < 1){
  42.           System.out.print("Please enter day between 1 to 30.");
  43.           System.exit(0);
  44.         }
  45.       }
  46.       else{
  47.         if(new GregorianCalendar().isLeapYear(year)){
  48.           if(day < 1 || day > 29){
  49.             System.out.print("Please enter day between 1 to 29.");
  50.             System.exit(0);
  51.           }
  52.         }
  53.         else if(day < 1 || day > 28){
  54.           System.out.print("Please enter day between 1 to 28.");
  55.           System.exit(0);
  56.         }
  57.       }
  58.       if(year == cd.get(Calendar.YEAR)){
  59.         if(month == cd.get(Calendar.MONTH)){
  60.           if(day > cd.get(Calendar.DAY_OF_MONTH)){
  61.             System.out.print("Invalid date!");
  62.             System.exit(0);
  63.           }
  64.         }
  65.       }
  66.     }
  67.     catch(NumberFormatException ne){
  68.       System.out.print(ne.getMessage() + " is not a legal entry!");
  69.       System.out.print("Please enter number.");
  70.       System.exit(0);
  71.     }
  72.     Calendar bd = new GregorianCalendar(year, month, day);
  73.     ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
  74.     if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
  75.       ageYears--;
  76.       ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
  77.       if(day > cd.get(Calendar.DAY_OF_MONTH)){
  78.         ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
  79.       }
  80.       else if(day < cd.get(Calendar.DAY_OF_MONTH)){
  81.         ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
  82.       }
  83.       else{
  84.         ageDays = 0;
  85.       }
  86.     }
  87.     else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
  88.       ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
  89.       if(day > cd.get(Calendar.DAY_OF_MONTH))
  90.         ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
  91.       else if(day < cd.get(Calendar.DAY_OF_MONTH)){
  92.         ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
  93.       }
  94.       else
  95.         ageDays = 0;
  96.     }
  97.     else{
  98.       ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
  99.       ageMonths = 0;
  100.       ageDays = 0;
  101.     }
  102.     System.out.print("Age of the person : " + ageYears + " year, " + ageMonths + 
  103. " months and " + ageDays + " days.");
  104.   }
  105. }
  106.  

this the code i have but i want to simply by this please help me
Feb 15 '08 #7
r035198x
13,262 8TB
What did you say the problem is?
Does the code compile sucessfully?
Does it give the correct results?
Feb 15 '08 #8
hirak1984
316 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2.     Calendar bd = new GregorianCalendar(year, month, day);
  3.     ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
  4.     if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
  5.       ageYears--;
  6.       ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
  7.       if(day > cd.get(Calendar.DAY_OF_MONTH)){
  8.         ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
  9.       }
  10.       else if(day < cd.get(Calendar.DAY_OF_MONTH)){
  11.         ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
  12.       }
  13.       else{
  14.         ageDays = 0;
  15.       }
  16.     }
  17.     else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
  18.       ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
  19.       if(day > cd.get(Calendar.DAY_OF_MONTH))
  20.         ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
  21.       else if(day < cd.get(Calendar.DAY_OF_MONTH)){
  22.         ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
  23.       }
  24.       else
  25.         ageDays = 0;
  26.     }
  27.     else{
  28.       ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
  29.       ageMonths = 0;
  30.       ageDays = 0;
  31.     }
  32.     System.out.print("Age of the person : " + ageYears + " year, " + ageMonths + 
  33. " months and " + ageDays + " days.");
  34.   }
  35. }
  36.  

this the code i have but i want to simply by this please help me
Look.
first of all dont randomly pick up codes from the net. The code you posted is written by a professional, abiding by all the standards of code writing.
But not so affluent as you are, it is bound to look puzzling to you.
You need not take any user input at this point. Hard code a date in your main().
and try to calculated age using it.
use the formula they have used in the above part of your code.
tell us what errors you got in doing this.
Feb 15 '08 #9
karthickkuchanur
156 100+
Look.
first of all dont randomly pick up codes from the net. The code you posted is written by a professional, abiding by all the standards of code writing.
But not so affluent as you are, it is bound to look puzzling to you.
You need not take any user input at this point. Hard code a date in your main().
and try to calculated age using it.
use the formula they have used in the above part of your code.
tell us what errors you got in doing this.
ok sir i will try to do my best
Feb 15 '08 #10
JosAH
11,448 Expert 8TB
@OP: maybe just for the fun of it (or a better understanding of dates), google for
Zeller's congruence.

kind regards,

Jos
Feb 15 '08 #11
hirak1984
316 100+
@OP: maybe just for the fun of it (or a better understanding of dates), google for
Zeller's congruence.

kind regards,

Jos

wow!!!
that was gr8 man. really interesting.
thnx JosAH
Feb 18 '08 #12
Guys, here is the easiest code of age calculator...


Expand|Select|Wrap|Line Numbers
  1. import java.util.Calendar;
  2. import java.text.SimpleDateFormat;
  3.  
  4. public class age {
  5.  
  6.  private float year;
  7.  private float age;
  8.  private float month;
  9.  private float yearx;
  10.  private float monthx;
  11.  private float dayx;
  12.  private float day;
  13.  private float mon;
  14.  private String yearFormat = "yyyy";
  15.  private String monthFormat = "MM";
  16.  private String dayFormat = "dd";
  17.  
  18.     public age (int d ,int m, int y){
  19.         float curYear = Integer.parseInt(curDate(yearFormat));
  20.         float curMonth = Float.parseFloat(curDate(monthFormat));
  21.         float curDay = Float.parseFloat(curDate(dayFormat));
  22.         year = y;
  23.         month = m;
  24.         day = d;
  25.  
  26.          yearx = curYear - year;
  27.          monthx = curMonth - month;
  28.          dayx = curDay - day;
  29.          age = yearx +  monthx/12;
  30.          if( monthx < 0) {
  31.              mon = 12-(Math.abs(monthx));    
  32.          }
  33.          else {
  34.              mon = Math.abs(monthx);
  35.          }
  36.  
  37.     }
  38.  
  39.  
  40.   static String curDate(String format) {
  41.     Calendar cal = Calendar.getInstance();
  42.     SimpleDateFormat sdf = new SimpleDateFormat(format);
  43.     return sdf.format(cal.getTime());
  44.  
  45.   }
  46.  
  47.  
  48.  
  49.   public String toString () {
  50.  
  51.       return String.format("Age: %.0fyears %.0fmonths %.0f%s", Math.floor(age) ,  Math.abs((dayx>=0? mon:mon-1)) , (dayx>=0? dayx:30+dayx), ((Math.abs(dayx))>1? "days":"day") );
  52.   }
  53.  
  54. }
Jun 30 '10 #13
Expand|Select|Wrap|Line Numbers
  1. /**
  2.      * Computes the duration between an input date and date today and returns
  3.      * the value in years, months and days
  4.      * 
  5.      * @param month
  6.      *            - input month
  7.      * @param day
  8.      *            - input day
  9.      * @param year
  10.      *            - input year
  11.      * @return duration in years, months and days
  12.      */
  13.     public String getDuration(int month, int day, int year) throws Exception {
  14.         /* input calendar date */
  15.         month--; // following the 0-based rule
  16.         Calendar cal = new GregorianCalendar(year, month, day);
  17.  
  18.         /* date today */
  19.         java.util.Date today = new java.util.Date();
  20.  
  21.         /* year now */
  22.         SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
  23.         int intYear = Integer.parseInt(sdfYear.format(today));
  24.  
  25.         /* month now */
  26.         SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
  27.         int intMonth = Integer.parseInt(sdfMonth.format(today));
  28.         intMonth--; // following the 0-based rule
  29.  
  30.         /* day now */
  31.         SimpleDateFormat sdfDay = new SimpleDateFormat("dd");
  32.         int intDay = Integer.parseInt(sdfDay.format(today));
  33.  
  34.         /* calendar date now */
  35.         Calendar now = new GregorianCalendar(intYear, intMonth, intDay);
  36.  
  37.         /* years duration */
  38.         int yyyy = intYear - year;
  39.  
  40.         /* array of days in 12 months */
  41.         int[] months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  42.  
  43.         /*
  44.          * an indicator if given date has already occurred. (-1) - if given date
  45.          * is greater than the current date; 0 - if given date has not yet
  46.          * occurred or if it's equal to the current date.
  47.          */
  48.         int factor = 0;
  49.  
  50.         int mm = 0; // month duration
  51.  
  52.         int dd = 0; // day duration 
  53.  
  54.         /* determine if given date is greater than or equal to the current date */
  55.         if ((month > intMonth) || (month == intMonth && day > intDay)) {
  56.             factor = -1;
  57.             yyyy += factor;
  58.         }
  59.  
  60.         /* throw if any of the following exceptions occur */
  61.         if (month > 12) {
  62.             throw new Exception("Invalid input month");
  63.         } else if (day > months[month]) {
  64.             throw new Exception("Invalid input day");
  65.         } else if (yyyy < 0) {
  66.             throw new Exception("Invalid input date");
  67.         }
  68.  
  69.         /*
  70.          * if the given date has already passed or if it's equal to the current
  71.          * date
  72.          */
  73.         if (factor == 0) {
  74.             // compute for days in between the given and the current date
  75.             dd = now.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR);
  76.             /* determine if the current year is a leap year */
  77.             if ((intYear % 4) == 0) {
  78.                 months[1]++; // increment the days in February by 1
  79.             }
  80.             /*
  81.              * compute for day duration and month duration between the given
  82.              * date and the given day of the current month
  83.              */
  84.             for (int i = month; i <= intMonth; i++) {
  85.                 if (dd >= months[i]) {
  86.                     dd -= months[i];
  87.                     mm++;
  88.                 }
  89.             }
  90.             /*
  91.              * if computed month duration is more than 12, finalize values for
  92.              * year and month duration
  93.              */
  94.             if (mm >= 12) {
  95.                 yyyy += (mm / 12);
  96.                 mm %= 12;
  97.             }
  98.         } else { // if the given date is greater than the current date
  99.             intYear--; // derive previous year
  100.             /* set Calendar date for December 31 of the previous year */
  101.             Calendar prev = new GregorianCalendar(intYear, 11, 31);
  102.             /*
  103.              * compute the days in between the given date last year and the
  104.              * current date
  105.              */
  106.             dd = (prev.get(Calendar.DAY_OF_YEAR) - cal
  107.                     .get(Calendar.DAY_OF_YEAR))
  108.                     + now.get(Calendar.DAY_OF_YEAR);
  109.             /* determine if previous year is a leap year */
  110.             if ((intYear % 4) == 0) {
  111.                 months[1]++; // increment the days in February by 1
  112.             }
  113.             /*
  114.              * compute for day duration and month duration between the given
  115.              * date and the given day in December of the previous year
  116.              */
  117.             for (int i = month; i <= 11; i++) {
  118.                 if (dd >= months[i]) {
  119.                     dd -= months[i];
  120.                     mm++;
  121.                 }
  122.             }
  123.             intYear++; // set the value back to the current year
  124.             /* determine if current year is a leap year */
  125.             if ((intYear % 4) == 0) {
  126.                 months[1]++; // increment the days in February by 1
  127.             }
  128.             /*
  129.              * compute for day duration and month duration between the given day
  130.              * in January of the current year and the current date
  131.              */
  132.             for (int i = 0; i <= intMonth; i++) {
  133.                 if (dd >= months[i]) {
  134.                     dd -= months[i];
  135.                     mm++;
  136.                 }
  137.             }
  138.             /*
  139.              * if computed month duration is more than 12, finalize values for
  140.              * year and month duration
  141.              */
  142.             if (mm >= 12) {
  143.                 yyyy += (mm / 12);
  144.                 mm %= 12;
  145.             }
  146.         }
  147.  
  148.         /* computed duration in years, months and days */
  149.         return yyyy + " Year(s), " + mm + " Month(s), and " + dd + " Day(s) ";
  150.     }
Jul 28 '10 #14
Expand|Select|Wrap|Line Numbers
  1. /**
  2. * Another age calculater which calcultes the age in years (on the minute)
  3. * Just a quickly made (5 min) piece of code for who ever is interested
  4. **/
  5.  
  6. // Initialize a birth date calendar and set the birth date
  7. Calendar birthDateCalendar = Calendar.getInstance();
  8. birthDateCalendar.setTimeInMillis((new SimpleDateFormat("dd-MM-yyyy HH:mm").parse("13-12-1979 14:39").getTime()));
  9.  
  10. // Initialize the age with -1 : stating a future birth date
  11. int ageInYears = -1;
  12.  
  13. // Add a year until the birthdate equals now
  14. for(Calendar now = Calendar.getInstance(); birthDateCalendar.compareTo(now) < 0 ; birthDateCalendar.add(Calendar.YEAR, 1))
  15. {
  16.       ageInYears += 1;
  17. }
  18.  
  19. // Here's the age in years
  20. System.err.println(ageInYears);
  21.  
Oct 28 '10 #15
you can check this tutorial How to calculate age in java?. I am sure you will find it helpful.
Jan 23 '12 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by John Norvell | last post: by
3 posts views Thread by Kevin Gibbons | last post: by
6 posts views Thread by vijayk | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.