Calculate Age in months

Hi all ,
How to calclate the Age in Months
for example DOB is : 08/04/2007
And I need result like : 1 month 24 days (or 1.24)
I did something but its not getting correct..

Here is the code I tried
    private void ageInMonths()
     String dateOfBirth = "08/04/2007";
      double ageMonths = 0.0;
      try
        int pYear = Integer.parseInt(dateOfBirth.substring(6));
        int pMonth = Integer.parseInt(dateOfBirth.substring(0, 2));
        int pDay = Integer.parseInt(dateOfBirth.substring(3, 5));
        Calendar cal = new GregorianCalendar();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        cal = null;
        ageMonths = (year - pYear) * 12.0 + (month - pMonth);
        int idiff = day - pDay;
        int ageDays = (idiff >= 0 ? idiff : -idiff);
        System.out.print("Months:"+ageMonths);
        System.out.print("Days:"+ageDays);
      catch (NumberFormatException ex)
       System.out.println("Exception");
    }

Similar Messages

  • Function Module to calculate age

    Hi Abapers,
    Kindly suggest some function module which can calculate age of a person by taking date of birth till date.
    Thanks in advance,
    Radhika

    Hi Radhika,
    Check this program
    REPORT ZTEST.
    DATA : BIRTH_DATE LIKE SY-DATUM,
           DAYS TYPE NUM2,
           MONTHS TYPE NUM2,
           YEARS TYPE NUM2.
    BIRTH_DATE = '19830307'.
    CALL FUNCTION 'HRCM_TIME_PERIOD_CALCULATE'
      EXPORTING
        BEGDA         = BIRTH_DATE
        ENDDA         = SY-DATUM
      IMPORTING
        NOYRS         = YEARS
        NOMNS         = MONTHS
        NODYS         = DAYS
      EXCEPTIONS
        INVALID_DATES = 1
        OVERFLOW      = 2
        OTHERS        = 3.
    WRITE : / YEARS , 'years' , MONTHS , 'months' , DAYS , 'days'.
    Regards,
    Satish

  • How to calculate age in oracle

    Hi,
    T was trying do calculate age as on todays date in ORACLE but after a lot of brain storming i didn't get it.
    Kindly tell me how to calculate age as in years month days.
    For ex.. My DOB- 02-feb-1984 so my age should get as 27 Years 2 months 8 days
    How to do it.
    I tried this
    select
    TRUNC( months_between( sysdate, TO_DATE('02-02-1984','DD-MM-YYYY') )/12 ) Year,
    TRUNC( mod(months_between( sysdate, TO_DATE('02-02-1984','DD-MM-YYYY') ),12) ) Month,
    mod(months_between( sysdate, TO_DATE('02-02-1984','DD-MM-YYYY') ),12) /30 Days
    from dual
    but days are not calculating correctly...
    RGds,
    PC

    sorry..i didn't chck your query..it is right but give wrong answer..
    WITH got_months AS
         SELECT     TO_DATE('02-02-1984','DD-MM-YYYY')
         ,     FLOOR (MONTHS_BETWEEN (SYSDATE, TO_DATE('02-02-1984','DD-MM-YYYY')))     AS months
         FROM     dual
    SELECT     TO_DATE('02-02-1984','DD-MM-YYYY')
    ,     TO_CHAR (FLOOR (months / 12))     || ' years, '     ||
         TO_CHAR (MOD (months, 12))      || ' months, '     ||
         TO_CHAR ( CEIL ( SYSDATE
              - ADD_MONTHS ( TO_DATE('02-02-1984','DD-MM-YYYY')
                        , months
              )               || ' days'
    FROM got_months
    o/p
         TO_DATE('02-02-1984','DD-MM-YY     TO_CHAR(FLOOR(MONTHS/12))||'YE
    1     2/2/1984     27 years, 7 months, 9 days
    1 day more..1     it sud be.. 2/2/1984     27 years, 7 months, 8 days
    if i make correction with add_months like below..
    WITH got_months AS
         SELECT     TO_DATE('02-02-1984','DD-MM-YYYY')
         ,     FLOOR (MONTHS_BETWEEN (SYSDATE, TO_DATE('02-02-1984','DD-MM-YYYY')))     AS months
         FROM     dual
    SELECT     TO_DATE('02-02-1984','DD-MM-YYYY')
    ,     TO_CHAR (FLOOR (months / 12))     || ' years, '     ||
         TO_CHAR (MOD (months, 12))      || ' months, '     ||
         TO_CHAR ( CEIL ( SYSDATE
              - ADD_MONTHS ( TO_DATE('02-02-1984','DD-MM-YYYY')
                        , months
                        )-1
              )               || ' days'
    FROM got_months
    then o/p is..
         TO_DATE('02-02-1984','DD-MM-YY     TO_CHAR(FLOOR(MONTHS/12))||'YE
    1     2/2/1984     27 years, 7 months, 8 days
    correct but cdnt understand why sud -1?

  • Calculate age in adf

    Gurus,
    Any inbuilt way to calculate age in adf in year, month format ?
    thnks

    Its not perfect, but in the SummitADF application I calculate a time to ship as
    public oracle.jbo.domain.Number calculateTimeToShip(Date ordered,
    Date shipped) {
    if (null != shipped) {
    long days =
    (shipped.getTime() - ordered.getTime()) / (1000 * 60 * 60 *
    24);
    return new Number(days);
    } else
    return new Number(0);
    You can ammend this to work off of the current date.
    Also a quick google shows loads of examples
    e.g.
    http://www.exampledepot.com/egs/java.util/GetAge.html

  • How to calculate Age in BEx query

    Hi,
    I need to calculate age in years based on person's date of birth and the key date that user of the report will enter.
    So, basically in my query I already have date of birth characteristic from 0PERSON InfoObject. I created ZAGE key figure and added it to the query that I would like to populate with person's age.
    So I know I have to enter some code in CMOD for calculated key figure ZAGE to get calculated based on birthdate and key date that user enters. Can somebody help with the code?
    thanks

    Hi,
    The variable should be in "Date" dimension. I have missed one important information in earlier mail.
    The processing type of the variable (user entry date) should be of "Customer Exit' and dimension (in last tab) should be "Date".
    By selecting this dimension, the variable input help screen also appears in calendar format to help user to select date instead of writing it in variable input.
    No need to write any custom code for this variable. We are selecting the "Customer exit" processing type to get Date dimension. In Normal user entry variables we can't get date & time dimension.
    "Ready for Input" should be selected so that user can enter date.
    I hope you can solve your issue now.
    Thanks for your feedback.
    Regards,
    Arun Thangaraj.
    Edited by: Arun  Thangaraj on Oct 8, 2008 8:48 AM

  • How to Calculate last 12 months in webi?

    Hi,
    I am working on a webi report based on oracle. My report table does not have any month field in it. Hence, I created a derived table in the universe for 12 months. Now, in my report I need to calculate last 12 months data. The month and year value will be given by the user and from that date values will be displayed for previous 12 months on a chart.
    Any ideas how achieve this in webi?
    Regards,
    C Mira

    Hi,
    Generally you can get the user values using the UserResponse() function.
    Using this, you can do some basic transformation of the user values for month and year to create a "Year_month_user" variable - e.g. in the format YYYYMM or better yet - create a date variable!
    (using the ToDate() function)
    When this variable is created, you can create a similar variable "Year_month_unv" from the universe objects.
    Then apply a custom filter on your chart with the syntax
    =[Year_month_unv] Between([Year_month_user];RelativeDate([Year_month_user];-365))
    I hope this helps.
    Btw: my first forum post - Yes!
    Edited by: Simon Steiper on Jul 28, 2010 2:25 PM (added the RelativeDate to syntax)

  • Function module which could calculate the previous month's start date.....

    hi,
    I want a function module which could calculate the previous month's start date and end date...
    Say todays date is  like 29.05.2007(start date) it should return
    01.04.2007 and 30.04.2007...
    IS there any FM for this ... or how to go about this scenario ???
    thanks in advance
    samm

    See the below Logic :
    DATA :g_date(2) TYPE n,           " Date
          g_month(2) TYPE n,          " Month
          g_year(4) TYPE n ,          " Year
          g_bill_low(10) TYPE n,       " From date
          g_bill_high(10) TYPE n,      " To date
          g_month1(2) TYPE n,         " Month
          g_year1(4) TYPE n,          " Year
          g_date1(2) TYPE n,          " Date
          g_year2(4) TYPE n,          " Year
          g_datum LIKE sy-datum.      " System date
    RANGES : r_bdate  FOR vbrk-fkdat.             " Billing date
      g_datum = p_date + 10.
      g_month = g_datum+4(2).
      g_year = g_datum+0(4).
      IF g_month = 1.
        g_year = g_year - 1.
        g_month = 12.
        g_date = 1.
      ELSE.
        g_month = g_month - 1.
        g_date = 1.
      ENDIF.
    Passing the date to billing date-low
      CONCATENATE  g_year g_month g_date  INTO g_bill_low.
      r_bdate-low = g_bill_low.
      r_bdate-sign = 'I'.
      r_bdate-option = 'BT'.
      g_month1 = g_datum+4(2).
      g_year1 = g_datum+0(4).
      IF g_month1 = 1.
        g_year1 = g_year1 - 1.
        g_month1 = 12.
      ELSE.
        g_month1 = g_month1 - 1.
      ENDIF.
      CASE g_month1.
        WHEN 1.g_date1 = '31'.
        WHEN 3.g_date1 = '31'.
        WHEN 4.g_date1 = '30'.
        WHEN 5.g_date1 =  '31'.
        WHEN 6.g_date1 = '30'.
        WHEN 7.g_date1 = '31'.
        WHEN 8.g_date1 = '31'.
        WHEN 9.g_date1 = '30'.
        WHEN 10.g_date1 = '31'.
        WHEN 11.g_date1 = '30'.
        WHEN 12.g_date1 = '31'.
      ENDCASE.
      g_year2 = g_year1.
      IF g_month1 = 2.
        g_year2 = g_year2 MOD 4 .
        IF g_year2 = 0.
          g_date1 = 29.
        ELSE.
          g_date1 = 28.
        ENDIF.
      ENDIF.
    Passing the date to billing date-high
      CONCATENATE  g_year1  g_month1  g_date1 INTO g_bill_high.
      r_bdate-high = g_bill_high.
      APPEND r_bdate.
    Reward Points if it is helpful
    Thanks
    Seshu

  • ABAP-HR MODULE(HOW TO CALCULATE AGE & YEARS OF SERVICE)

    HI,
    HOW TO CALCULATE AGE & YEARS OF SERVICE means for example
    I AM USING PNPCE LDB.
    (1) whose age is greater than 52.833 years with 7.833 years of service, with annual rate of pay $170,000 or more, or
    (2) age plus employment service is 65 or more, with annual rate of pay of $ 170,000 or more.
    Note that the $ 170,000 parameter would be a variable that could change annually when this report would be generated. In the past, age and service value were determined as of the run date. The determination date would also be a variable that would change when the report would be generated.
    Thanks&Regards
    Rahul.

    Hi Rahul,
    This is the 2nd warning !!! Please... use the correct or most appropriate forum.
    The ABAP Objects Forum should be used for: ABAP Object definition and implementation including encapsulation, interfaces and inheritance in ABAP Objects.
    This thread will be moved from to .
    You're asking this question in two thread, so the duplicated thread will be deleted.
    Please have a look at [Forum Rules of Engagement|https://wiki.sdn.sap.com/wiki/display/HOME/RulesofEngagement] before posting.
    Also read this thread Welcome and Rules of Engagement.
    Greetings,
    Marcelo Ramos

  • With 'Revenue' Fact Column how can i calculate 'Previous 3 Months Revevue'

    Hi All ,
    i have a Column Called Revenue from Fact Table. i need to calculate Previous 3 Months Revevue
    How can i achive this ? please help me
    Thanks in advance

    Hi,
    Create one new logical coloumn bmm layer and
    1.Check Use existing logical columns as the source
    2.Click the Expression Builder button (three dots) to open the Expression Builder
    3.Select Functions > Time Series Functions > Ago.
    by using this u can create 3 months year ago revenue coloumn and use this coloumn in your formula
    hope this will help for you
    Naresh

  • How to Calculate AGE by getting difference between two Date Fields

    HI Gems
    I need to calculate AGE from getting difference from two date fields. But when i am trying to wrte fromula as Current date - date1(some date field) then it is showing error.
    How can i get values.
    Thanks
    Manu

    You already asked this question:
    How to calculate AGE from two different date fields

  • How to calculate AGE from two different date fields

    hi
    I need to calculate AGE from two different date fields.
    Can some help me how to do, when i try to do substraction formula it is showing error.
    Thank You
    Manu

    Manu wrote:
    Hi
    Both fields are in date format only, i need to calculate no . of days between two different dates
    Thanks
    ManuThe reason for the question about the format of the column is because the simplest solution only works on DATE columns. Others have mentioned this here, but if you want the difference between two date fields, you can use this formula:
    TIMESTAMPDIFF(SQL_TSI_DAY, date_column1, date_column2)
    The above being said, you still didn't tell me what you did, or what error message you received. So again, if the above formula didn't work, what did you do? Where did you put the formula? What was the exact syntax you used? What was the error message you got?
    Please don't make us work more than we need to. Answer all the questions in your next post. Thanks.

  • How to calculate age in SQL?

    I have a table with DATE as one of the column.
    How do I calculate age?
    Thanks in advance
    -Shilpa

    michaels2 wrote:
    TryWell it still leaves leap year question open. If person was born on February 29, how old is that person on February 28 of a non-leap year? Your code considers it imcomplete year:
    SQL> var dob varchar2(20)
    SQL> exec :dob := '19960229';
    PL/SQL procedure successfully completed.
    SQL> select trunc ( (to_number (to_char (to_date('20090228','yyyymmdd'), 'yyyymmdd'))
      2                  - to_number (to_char(to_date(:dob, 'yyyymmdd'),'yyyymmdd'))
      3                  )
      4                / 10000)
      5            as "Age of Child"
      6    from dual
      7  /
    Age of Child
              12SY.

  • Calculate age in oracle 11g

    how do i calculate age based on date picker in another field on the same table/

    Are you sure of that algorithm?
    orclz> SELECT Round((SYSDATE-to_date('01-JAN-1989','dd-MON-yyyy'))/365) FROM DUAL;
    ROUND((SYSDATE-TO_DATE('01-JAN-1989','DD-MON-YYYY'))/365)
                                                           25
    orclz> SELECT Round((SYSDATE-to_date('01-DEC-1989','dd-MON-yyyy'))/365) FROM DUAL;
    ROUND((SYSDATE-TO_DATE('01-DEC-1989','DD-MON-YYYY'))/365)
                                                           24
    How about this:
    orclz> select extract (year from sysdate) - extract( year from to_date('01-DEC-1989','dd-MON-yyyy')) from dual;
    EXTRACT(YEARFROMSYSDATE)-EXTRACT(YEARFROMTO_DATE('01-DEC-1989','DD-MON-YYYY'))
                                                                                24
    orclz> select extract (year from sysdate) - extract( year from to_date('01-JAN-1989','dd-MON-yyyy')) from dual;
    EXTRACT(YEARFROMSYSDATE)-EXTRACT(YEARFROMTO_DATE('01-JAN-1989','DD-MON-YYYY'))
                                                                                24
    orclz>

  • How to calculate next 12 months

    Hi Gurus,
    If a user enters a date like : 15.05.2007, I need to calculate next 12 months values.
    i.e. low = 16.05.2007
         high= 15.05.2008.
    Any help greatly appreciated. Thanks.
    Best Regards,
    Reddy.

    data: l_month         type i,
            l_year          type i,
            l_sum_of_months type i.
    data : V_DATE TYPE  D,
             V_MONTHS TYPE  I DEFAULT 0,
             E_DATE TYPE  D.
        l_sum_of_months = v_date+4(2) + v_months.               "YREM981595
        l_month         = ( l_sum_of_months + 1 ) mod 12.       "YREM981595
        if l_month eq 0.
          l_month = 12.
        endif.
        l_year  = l_sum_of_months div 12.
        l_year  = l_year + v_date(4).
        if l_year gt 9999.
          e_date      = con_hidate.
        else.
          e_date+6(2) = '01'.
          e_date+4(2) = l_month.
          e_date(4)   = l_year.
          e_date      = e_date - 1.
        endif.
    rewards if useful,
    regards,
    nazeer

  • Calculate Age in an ABAP report

    Hi all you wonderful SAP people,
    I am trying to calculate age in my ABAP report,
    Any function module I can use or code snippet? Your help is much appreciated. Points will be awarded.
    Regards
    Ramorua

    Hi Mohamed,  some characters (plus signs, and spaces around the minus) seem to have gotten lost from your code, maybe during the conversion of the forum. Also, the data type is i, not int.
    Header 1
          DATA: lv_age TYPE i.
          lv_age = sy-datum(4) - lv_birthdate(4).
          IF sy-datum+4(4) LT lv_birthdate+4(4).
            lv_age = lv_age - 1. " or SUBTRACT 1 FROM lv_age.
          ENDIF.

Maybe you are looking for