Results 1 to 6 of 6

Thread: Calculating Age in java

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Calculating Age in java

    Hello,
    How can I retrieve the system date in java and calculate the age of a person knowing his date of birth? If possible, give me the full syntax. Any idea regarding this topic will be highly appreciated. Thank you in advance

  2. #2
    Join Date
    Nov 2009
    Posts
    446

    Re: Calculating Age in java

    Hello,
    For the current date in milliseconds:
    long test = System.currentTimeMillis();
    to the date of birth:
    Code:
    java.util.Calendar Brh = Calendar.getInstance();
    Then you have to play with the setters, and it retrieves the date in milliseconds as follows:
    Code:
    long test1 = birth.getTime();
    Subtraction will do for the rest and I'll leave it to the conversion years.

  3. #3
    Join Date
    Nov 2009
    Posts
    359

    Re: Calculating Age in java

    Hello,
    You can extract the years, get (Calendar.YEAR). You will get and the year of birth and the current year, example
    Code:
    February 13, 1985 -> 1985
    October 22, 2009 -> 2009
    In this case the person in 24 years, then uses the method add (Calendar.YEAR, -24), and compares the date, removes one years if 24 years before the person is not born: she was born after October 22.

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Calculating Age in java

    Hello,
    For the above explanation, I have developed a code, please check it out. Hope this will help you.
    Code:
    Public static int getYear(Date of)
    {
      Calendar cu = Calendar.getInstance();
      Calendar brt = Calendar.getInstance();
      brt.setTime(d);
      int yeardiff = cu.get(Calendar.YEAR) - Birth.get(Calendar.YEAR);
      cu.add(Calendar.YEAR,-yeardiff);
      if(brt.after(cu))
      {
        yeardiff = yeardiff -- 1;
      }
      return yeardiff;
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Calculating Age in java

    Hello,
    In the calendar class there are set methods that allow all precisely define what we want:
    Code:
    set(int year, int month, int date)
    So you create an object Calendar Using the method static Calendar getInstance ()Then you change that date at its convenience. When you look in the javadoc, which function returns a date: you need a date. You should not even need to read the javadoc for this finding: it is in my program, but still Surprise is getTime (). So after long search in the page cited above we find the function getTime (), so you get the Date object.

  6. #6
    Join Date
    Nov 2009
    Posts
    359

    Re: Calculating Age in java

    Hello,
    Please see the following code, I hope it will help you
    Code:
    import java.util.Calendar;
    import java.util.Date;
    class Test2
    {
      Public static int getYear(Date of)
      {
        Calendar cu = Calendar.getInstance();
        Calendar brt = Calendar.getInstance();
        brt.setTime(d);
        int yeardiff = cu.get(Calendar.YEAR) - Birth.get(Calendar.YEAR);
        cu.add(Calendar.YEAR,-yeardiff);
        if(brt.after(cu))
        {
          yeardiff = yeardiff -- 1;
        }
        return yeardiff;
      }
     
      Public static void hand(String[] args)
      {
        if(args.length != 3) { System.err.System.out.println("Standard"); System.exit(1); }
        Calendar ca2 = Calendar.getInstance();
        ca2.set(Integer.parseInt(args[0]), Integer.parseInt(args[1])-1, Integer.parseInt(args[2]));
        Date bday = ca2.getTime();
        System.out.System.out.println("The age of the master:"+ getYear(bday));
      }
    }

Similar Threads

  1. Need help calculating Age in FileMaker Pro
    By NathanDS in forum Windows Software
    Replies: 4
    Last Post: 29-05-2010, 05:16 PM
  2. Problem calculating duration in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 03:56 AM
  3. Calculating the sum of two polynomials
    By Liquid Shadow in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 11:12 AM
  4. Calculating PC Power Consumption
    By Justvicks in forum Hardware Peripherals
    Replies: 4
    Last Post: 01-12-2008, 01:19 PM
  5. Calculating frequency in RPM with JAVA
    By gvBlake in forum Software Development
    Replies: 2
    Last Post: 23-08-2008, 06:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •