Convert two dates difference to number of years, months and days

Post Author: gigimonu
CA Forum: Formula
I wanted to write a formula (if there is a function I can use) that can convert a date difference to total number of years, months and days example
adate = 10/22/2006
?xdate = current date - adate (answer should be 1 years, 0 months and 0 days)
Please help
Thanks

Post Author: V361
CA Forum: Formula
[Years, Months, Days]
DATEVAR FROMDATE := DATE(2000,01,01); // FROM DATE
DATEVAR TODATE := CURRENTDATE; // TO DATE
NUMBERVAR YEARS;
NUMBERVAR MONTHS;
NUMBERVAR DAYS;
STRINGVAR DIFF;
DATEVAR TEMP;
IF TODATE < FROMDATE THEN
(TEMP := TODATE;
TODATE := FROMDATE;
FROMDATE := TEMP);
YEARS := DATEDIFF('YYYY',FROMDATE,TODATE);
IF YEARS > 2 THEN
(YEARS := YEARS - 2;
TEMP := DATE(DATEADD("M",YEARS * 12,FROMDATE));)
ELSE
(YEARS := 0;
TEMP := FROMDATE);
WHILE TRUE DO
(TEMP := DATE(DATEADD('M',1,TEMP));
IF TEMP > TODATE THEN
EXIT WHILE;
MONTHS := MONTHS + 1);
DAYS := DATEDIFF('D',DATE(DATEADD('M',-1,TEMP)),TODATE);
IF MONTHS > 12 THEN
(YEARS := YEARS + INT(MONTHS/12);
MONTHS := MONTHS MOD 12);
DIFF := IIF(YEARS>0 ,TRIM(TOTEXT(YEARS,0)) & " YEARS " ,"0 YEARS ") &
IIF(MONTHS>0,TRIM(TOTEXT(MONTHS,0))& " MONTHS ","0 MONTHS ")&
IIF(DAYS>0 ,TRIM(TOTEXT(DAYS,0)) & " DAYS" ,"0 DAYS");

Similar Messages

  • How to get date difference in terms of years,months and also days

    Tool : Eclipse
    Framework : JSF & Springs
    JRE : jdk1.5.0_06
    Iam using oracle 10G DB as back end.I have two date fields in a table.
    1)premium_paying_start_date
    2)premium_paying_end_date
    Iam getting these two dates from the database and storing these values within a entity.Now in the delegate layer,
    i have to get the premium_term i.e, the difference between the two dates(premium_paying_end_date-premium_paying_start_date).
    The difference should show the year,month and no of days difference.
    For example :
    premium_paying_start_date : 14-10-1984
    premium_paying_end_date : 01-03-2008
    Difference should be : 23 Y : 4 M : 15 D (Y = years, M = months , D= days)
    So please give me the solution for this.

    Difference should be : 23 Y : 4 M : 15 D (Y = years, M = months , D= days)
    So please give me the solution for this.How did you determine what the difference should be?
    ~

  • Converting a number into years, months and days...

    Hi everyone,
    I have a number say 399.
    I need to know how many years and months constitute that number.
    For eg 399 would be 1yr, 1 month and 4 days.
    So I need the answer as 1 yr and 1 month.
    Is there any inbuilt funtion to say this?
    Thanks
    Kishore

    following a quick (and dirty) solution.
    What about the leap years?
    define v_startdate='01-01-2003'
    define v_juliandays=399
    SELECT
    TRUNC (         MONTHS_BETWEEN ( TO_DATE('&&v_startdate','MM-DD-YYYY') + &&v_juliandays,
                                     TO_DATE('&&v_startdate','MM-DD-YYYY')
                                   )/12) "Years",
    TRUNC ( MOD (   MONTHS_BETWEEN ( TO_DATE('&&v_startdate','MM-DD-YYYY') + &&v_juliandays,
                                     TO_DATE('&&v_startdate','MM-DD-YYYY')
                                   ) ,12)) "Months"
    FROM DUAL;

  • How to get the difference of two dates in years,months and days

    Hi friends,
    how to get the difference of two dates in years,months and days
    for ex 2 years 3 months 13 days
    select (sysdate-date_Start) from per_periods_of_service
    thanks

    Something like this...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select to_date('17-nov-2006','dd-mon-yyyy') as c_start_date, to_date('21-jan-2008','dd-mon-yyyy') as c_end_date from dual union all
      2             select to_date('21-nov-2006','dd-mon-yyyy'), to_date('17-feb-2008','dd-mon-yyyy') from dual union all
      3             select to_date('21-jun-2006','dd-mon-yyyy'), to_date('17-jul-2008','dd-mon-yyyy') from dual
      4             )
      5  -- end of test data
      6  select c_start_date, c_end_date
      7        ,trunc(months_between(c_end_date, c_start_date) / 12) as yrs
      8        ,trunc(mod(months_between(c_end_date, c_start_date), 12)) as mnths
      9        ,trunc(c_end_date - add_months(c_start_date, trunc(months_between(c_end_date, c_start_date)))) as dys
    10* from t
    SQL> /
    C_START_D C_END_DAT        YRS      MNTHS        DYS
    17-NOV-06 21-JAN-08          1          2          4
    21-NOV-06 17-FEB-08          1          2         27
    21-JUN-06 17-JUL-08          2          0         26
    SQL>But, don't forget that different months have different numbers of days, and leap years can effect it too.

  • FM to get the number of days in Year,month and days by giving number of day

    Hi ALL,
    This is quit differnt.
    I need to give input the 'start date' and the 'number of days' and get the total days from the start date in year,month and day format.
    for example.
    start date :01.01.2009
    number of days as 32
    then i should get
    years:0
    months :1
    days :1
    Pleas help me out.

    hi Anusha,
    first u pass the date and the days to the following fm you will get the result date....
    data:date type sy-datum,
          r_date(10) type c.
    date = sy-datum.
    CALL FUNCTION 'CALCULATE_DATE'
    EXPORTING
       DAYS              = '32'
       MONTHS            = '0'
       START_DATE        = date
    IMPORTING
       RESULT_DATE       = r_date
    write:/ r_date.
    then you need to pass the result date and the date to the following fm to get the required output...
    CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'
        EXPORTING
          date1                   = r_date
          date2                   = date
        IMPORTING
          years                   = v_years
         months                 = v_months
        days                     = v_days
        EXCEPTIONS
          invalid_dates_specified = 1
          OTHERS                  = 2.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
    here u will get the difference in days,  months and year...
    i hope u wil get help from this...
    regards
    Ashu  Singh

  • Date separate year, month and day

    Hello everyone and thanks in advance, I'm new to this and the truth is not doing this.
    Well, I have a PDF with several pages which I designed and has been asking me in a complete date in the format dd / mm / yy and subsequent pages he asks me the same date of birth but in 3 separate fields, day , month and year. I want you when you enter the date the first copy and separate day month and year in others. I tried this but I get
    var array_datesol = datesol.split("/")
    var year = parseInt(array_datesol[2]);
    var month = parseInt(array_datesoll[1]);
    var day = parseInt(array_datesol[0]);
    this.getField('yearsol').value = year;
    this.getField('monthsol').value = month;
    this.getField('daysol').value = day;
    where is the initial field datesol date and year, month and day where they have to be separately
    thanks in advance

    Are you getting any errors in the JavaScript debugging console?
    You are using the syntax for the 'Custom JavaScript calculation' and this syntax can not be used in the 'simplified field notation. Also you can not use any JavaScript properties, objects, functions, control statments.
    When run in the console, I find you have a misspelling of the variable name 'datesol'. I also do not see where you are initializing the value for 'datesol'.
    var datesol = event.valueAsString;  // get value of this field
    var array_datesol = datesol.split("/") ; // make into an array
    this.getField('yearsol').value =array_datesol[2];  // get element year
    this.getField('monthsol').value = array_datesol[1];  // get month element - fixed variable name
    this.getField('daysol').value = array_datesol[0]; // get date element
    I would use one of the following so the other fields are only updated when a date has been entered and the leading zeros are displayed:
    this.getField('yearsol').value = '';
    this.getField('monthsol').value = '';
    this.getField('daysol').value = '';
    if(event.value != '') {
    // get parts of date and print with leading zeros
    var datesol = event.value;  // get value of this field
    var array_datesol = datesol.split('/') ; // make into an array
    this.getField('yearsol').value = util.printf('%,304.0f', array_datesol[2]);  // get element year
    this.getField('monthsol').value = util.printf('%,302.0f', array_datesol[1]);  // get month element - fixed variable name
    this.getField('daysol').value = util.printf('%,302.0f', array_datesol[0]); // get date element
    // or
    this.getField('yearsol').value = '';
    this.getField('monthsol').value = '';
    this.getField('daysol').value = '';
    if (event.value != '') {
    // get date parts and print with leading zeros
    var oDatesol = util.scand('dd/mm/yyyy', event.value); // get date object
    this.getField('yearsol').value = util.printf('%,302.0f',oDatesol.getFullYear());  // get element year
    this.getField('monthsol').value = util.printf('%,302.0f', oDatesol.getMonth() + 1);  // get month element - fixed variable name
    this.getField('daysol').value = util.printf('%,302.0f', oDatesol.getDate()); // get date element
    // or
    this.getField('yearsol').value = '';
    this.getField('monthsol').value = '';
    this.getField('daysol').value = '';
    if(event.vaue != '') {
    // get parts of date and format
    var oDatesol = util.scand('dd/mm/yyyy', event.value); // get date object
    this.getField('yearsol').value = util.printd('yyyy', oDatesol); // get element year
    this.getField('monthsol').value = util.printd('mm', oDatesol); // get month element - fixed variable name
    this.getField('daysol').value = util.printd('dd', oDatesol); // get date element
    If you are going to fill other field, you will need to use the '.valueAsStriing' property or '.toString()'  method to retain the leading zeros.

  • Difference between 2 dates in form of years,months and days.

    Hi friends,
      i need one function module for finding difference between 2 dates and the output is
    no. of years
    no.of months
    no. of days.
    it means the input is begin date and end date
    output is no.of years,no.of months and no.of days.
    for example begin date is today i.e., 08/02/2008 (dd/mm/yyyy) and end date is 07/03/2008 then the output is
    no.of years -
      0,
    no.of months----
      1,
    no.of days----
      0.
    and one more example if begin date 08/02/2008
    (dd/mm/yyyy) and end date is 01/02/2008 then the output is
    no.of years -
    0,
    no.of months----
      0,
    no.of days----
    -7..
    Please help me it's very urgent

    Hi,
    Use FM HR_AUPBS_MONTH_DAY.
    It Calculate the years, months, days & cal days between 2 dates.
    Hi,
    The FM is 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
    Check the following example:
    DATA: EDAYS LIKE VTBBEWE-ATAGE,
    EMONTHS LIKE VTBBEWE-ATAGE,
    EYEARS LIKE VTBBEWE-ATAGE.
    PARAMETERS: FROMDATE LIKE VTBBEWE-DBERVON,
    TODATE LIKE VTBBEWE-DBERBIS DEFAULT SY-DATUM.
    call function 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
    exporting
    i_date_from = FROMDATE
    i_date_to = TODATE
    I_FLG_SEPARATE = ' '
    IMPORTING
    E_DAYS = EDAYS
    E_MONTHS = EMONTHS
    E_YEARS = EYEARS.
    WRITE:/ 'Difference in Days ', EDAYS.
    WRITE:/ 'Difference in Months ', EMONTHS.
    WRITE:/ 'Difference in Years ', EYEARS.
    INITIALIZATION.
    FROMDATE = SY-DATUM - 60.
    Also You can write your own function module for this..
    just extract day and month and year from the two dates and then get the difference.
    suppose date 1 - y1m1d1
    date 2 = y2m2d2
    data : y1(2) type n,
    m1(2) type n,
    d1(2) type n,
    y2(2) type n,
    m2(2) type n,
    d2(2) type n,
    y1 = date1(4).
    m1 = date1+4(2).
    d1 = date1+6(2).
    y2 = date2(4).
    m2 = date2+4(2).
    d2 = date2+6(2).
    then get the differnce between years and days and months and store them in ur variables
    Reward Points if found helpfull..
    Cheers,
    Chandra Sekhar.

  • I need the age to show in Years Months and Days

    Post Author: LOgle0917
    CA Forum: Formula
    NumberVar DoBVar := IIF((100 * MONTH({CHARTING.CHARTINGDATE}) + DAY({CHARTING.CHARTINGDATE})<(100 * MONTH({DEMOGRAPHICS.BIRTHDATE})+ DAY({DEMOGRAPHICS.BIRTHDATE}))),1, 0);NumberVar MthVar := (DATEDIFF("m",{DEMOGRAPHICS.BIRTHDATE},{CHARTING.CHARTINGDATE}) - DobVar) MOD 12;NumberVar YrsVar := DATEDIFF("yyyy",{DEMOGRAPHICS.BIRTHDATE},{CHARTING.CHARTINGDATE}) - DobVar;StringVar MthYrs := TOTEXT(YrsVar,0) + " years " + TOTEXT(MthVar,0) + " months";
    This returns Years and Months what do I need to add to it to return days?
    Thanks!

    Post Author: SKodidine
    CA Forum: Formula
    Here is a formula that I modified for CR from a Visual FoxPro function by Ramani Subramanian G.
    Create a separate formula to replace your calculations for datediff and paste this replacing the values of fromdate and todate with {DEMOGRAPHICS.BIRTHDATE} and{CHARTING.CHARTINGDATE} respectively.
    DATEVAR FROMDATE := DATE(2000,01,01);   // FROM DATE
    DATEVAR TODATE   := CURRENTDATE;        // TO DATE
    NUMBERVAR YEARS;
    NUMBERVAR MONTHS;
    NUMBERVAR DAYS;
    STRINGVAR DIFF;
    DATEVAR   TEMP;
    IF TODATE < FROMDATE THEN
    (TEMP     := TODATE;
    TODATE   := FROMDATE;
    FROMDATE := TEMP);
    YEARS := DATEDIFF('YYYY',FROMDATE,TODATE);
    IF YEARS > 2 THEN
    (YEARS := YEARS - 2;
    TEMP  := DATE(DATEADD("M",YEARS * 12,FROMDATE));)
    ELSE
    (YEARS := 0;
    TEMP  := FROMDATE);
    WHILE TRUE DO
    (TEMP := DATE(DATEADD('M',1,TEMP));
    IF TEMP > TODATE THEN
        EXIT WHILE;
    MONTHS := MONTHS + 1);
    DAYS := DATEDIFF('D',DATE(DATEADD('M',-1,TEMP)),TODATE);
    IF MONTHS > 12 THEN
    (YEARS  := YEARS + INT(MONTHS/12);
    MONTHS := MONTHS MOD 12);
    DIFF := IIF(YEARS>0 ,TRIM(TOTEXT(YEARS,0)) & " YEARS " ,"0 YEARS ") &
            IIF(MONTHS>0,TRIM(TOTEXT(MONTHS,0))& " MONTHS ","0 MONTHS ")&
            IIF(DAYS>0  ,TRIM(TOTEXT(DAYS,0))  & " DAYS"   ,"0 DAYS");

  • Mailed iCal attachment shows right date but clicking will put appointment on wrong year, month and day. Even names involved are wrong!

    Running Mountain Lion on MacBook Air and MacBook Pro. Suddenly a mailed iCal attachment showing the right date, time and attendees ends up years and months off on iCal with totally wrong attendiees, when clicked in Mail. iCal synced to iCloud. This may be very trivial for some of you but I need help, please.
    Thank you.

    I ran into this exact issue after migrating a time machine backup from an older macbook air to a new macbook pro.
    The following steps solved my issue:
    1) Backup your current mac
    2) Create a new user account on your mac, and login to that account.
    3) While logged into the new user account, right click the time machine drive and select "get info" > Click the lock icon at the lower right corner so that you can make permission changes. Then Click on the "+" sign and add your new user account to the "Sharing & Permissions" window. Be sure to grant your new user "Read & Write" privileges and when you have done so, click on the down arrow and select "apply to enclosed items" This step will allow you to copy the library folder from your old user account to the new account without hitting any permission issues.
    4) Download "TinkerTool" (An application that allows you to enable the viewing of any and all hidden files and folders on your local or mounted hard drives) and check the box that reads "Show Hidden and System Files" and then click "Relaunch Finder.
    5) Lastly copy the Library folder from the older user account on the time machine backup drive to the new user's library folder. Also be sure to copy your Desktop, Documents, Pictures, and Music folders to the new account as well. Open Mac mail and follow the instructions to import the previous accounts mail messages. Once you receive future ics attachments or calendar invites you should be able to click on them and load the proper date and time within your iCal application.
    This worked for me and I hope it works for you.
    Thanks,
    Chris Sampson

  • Why can't i get the correct DAY_OF_WEEK given year, month and day

    public String getPayrollRange(String whichhalf) throws SQLException{
    int month_today = today.get(Calendar.MONTH) + 1;
    int year_today = today.get(Calendar.YEAR);
    int day_today = today.get(Calendar.DAY_OF_MONTH);
    int end_day = 0;
    int dayofweek = 0;
    try{ connect();
    str = "SELECT value FROM DEFAULTS WHERE variable LIKE '"+whichhalf+"%cutoff'";
    rs = execSQL(str);
    if(rs!=null)
    { if(rs.next())
    end_day = rs.getInt("value");
    rs.close();
    dbCon.close();
    //--------------- THIS IS THE MOST IMPT PART -----------------------
    // i need to be able to set a Calendar object based on the integer variables
    // my problem is when i retrieve the DAY_OF_WEEK for the given_date variable
    Calendar given_date = new GregorianCalendar(year_today, month_today, 28);
    str = given_date.toString();
    }catch(Exception e) {
    e.printStackTrace();
    return str;
    }

    http://developer.java.sun.com/developer/qow/archive/152
    index.jsphi! the link was a great help but 'now' refers to the current date.
    my problem really is when a user specifies a given date and i'd like to get its DAY_OF_WEEK. i don't wanna use sql to get the DAYNAME.
    pls help!

  • Number weeks between two dates which are in same year or different year

    Hi,
    how to calculate the number of weeks between two dates?
    eg. '17-mar-2013' and '27-jun-2013' or '15-jun-2013' and '25-mar-2014'
    Thanks in advance.
    lukx

    A not tested "integer arithmetic" example (to be adjusted to your conceptions if feasible) covering only both dates in the same year and dates in successive years examples.
    consider Setting Up a Globalization Support Environment
    select case when substr(:start_date,1,4) = substr(:end_date,1,4)
                then to_number(to_char(to_date(:end_date,'yyyymmdd'),'iw')) -
                     to_number(to_char(to_date(:start_date,'yyyymmdd'),'iw'))
                when to_number(substr(:end_date,1,4)) - to_number(substr(:start_date,1,4)) = 1
                then to_number(to_char(trunc(to_date(:end_date,'yyyymmdd'),'yyyy') - 1,'iw')) -
                     to_number(to_char(to_date(:start_date,'yyyymmdd'),'iw')) +
                     to_number(to_char(to_date(:end_date,'yyyymmdd'),'iw')) + 1
           end iso_weeks_between
      from dual
    Regards
    Etbin
    the following seems to work for iso weeks treated as buckets
    with
    t as
    (select date '2013-03-17' d1,date '2013-06-17' d2 from dual union all
    select date '2003-12-31',date '2004-01-01' from dual union all
    select date '2004-12-31',date '2005-01-01' from dual union all
    select date '2005-12-31',date '2006-01-01' from dual union all
    select date '2013-12-29',date '2013-12-30' from dual union all
    select date '2016-01-03',date '2016-01-04' from dual union all
    select date '2013-06-15',date '2014-03-25' from dual
    select d1,
           to_number(to_char(d1,'iw')) low_iso_week,
           d2,
           to_number(to_char(d2,'iw')) high_iso_week,
           to_number(to_char(d2,'iw')) - to_number(to_char(d1,'iw')) iso_week_diff,
           case when to_number(to_char(d2,'iw')) - to_number(to_char(d1,'iw')) < 0
                then to_number(to_char(d2,'iw')) - to_number(to_char(d1,'iw')) +
                     case when to_char(trunc(d2,'yyyy'),'iw') = '01'
                          then 52
                          else to_number(to_char(trunc(d2,'yyyy'),'iw'))
                     end
                else to_number(to_char(d2,'iw')) - to_number(to_char(d1,'iw'))
           end iso_weeks_apart
      from t
    Message was edited by: Etbin
    with no Database at hand pencil and paper is too error prone to figure out iso week changes for varioud december - january periods

  • How to get two date difference in Day/hour/min/secs pl

    First of all,
    Hello to All, i am new to Oracle DB, but i have little experience in Other Database such as Sybase, sql server etc
    please kindly help me
    in DoctorChart table i have DocName,Id#,Citcotype,Medtype,AdmitDate,DischargeDate(both admitdate & Discharge date is date formatted columns)
    so how can i get the below differenceTime (which means Dischargedate-Admit Date in below format i.e, Day,Hours,Minutes,Seconds...)
    DoctorName, ID #, Citco type, MEDtype, DifferenceTime
    ========= === ======= ======= ===========
    Dr. KindaEmesko, 20045, Replace OutCard, ICH, 0 day 9 hours 11 minutes
    Dr. KindaEmesko, 20098, Replace OutCard, ICH, 1 day 2 hours 34 minutes
    Dr. KindaEmesko, 20678, Replace OutCard, ICH, 2 day 23 hours 52 minutes
    Dr. KindaEmesko, 20212, Replace OutCard, ICH, 4 day 1 hours 00 minutes
    Dr. KindaEmesko, 20345, Replace OutCard, BED, 3 days 14 hours 15 minutes
    Dr. KindaEmesko, 20678, Replace OutCard, BED, 9 days 21 hours 52 minutes
    Dr. KindaEmesko, 20015, Signature Overlay, Rest, 0 days 3 hours 29 minutes
    Dr. KindaEmesko, 45678, Signature Overlay, Rest, 0 days 1 hours 29 minutes
    Same way how can i get the Average Time for the above result (without ID columns)
    DoctorName, Citco type, MEDtype, Avg DifferenceTime
    ========= === ======= ======= ===========
    Dr. KindaEmesko, Replace OutCard, ICH, 2 day 1 hours 00 minutes
    Dr. KindaEmesko, Replace OutCard, BED, 6 days 17 hours 15 minutes
    Dr. KindaEmesko, Signature Overlay, Rest, 0 days 2 hours 29 minutes
    Please just tell me how to get the two dates difference as day/hour/minute format
    as well as how to get avg ( two dates difference as day/hour/minute format)
    Please Help me
    Thanks in advance

    Well, instead of all these suggested manipulations you can simply use intervals. Oracle uses day as a unit in date arithmetic, so Dischargedate-Admit Date is number od days between two dates. Built-in function numtodsinterval can be used to convert it into interval. For example:
    SQL> with t as (
      2             select trunc(sysdate) - 2 Admit_Date,
      3                    sysdate            Discharge_Date
      4               from dual
      5            )
      6  select  numtodsinterval(Discharge_Date - Admit_Date,'DAY') Hospital_Stay
      7    from  t
      8  /
    HOSPITAL_STAY
    +000000002 00:01:08.000000000
    SQL>  SY.

  • How to convert amount data element to number data element

    Dear Gurus,
    I have a requirement to get the data by calling BRF+ and then pass them to BAPI 'BAPI_ASSET_ACQUISITION_POST', in the BRF+ export structure, there is a field 'AMOUNT', I bind it to DDIC element 'BF_ANBTR', the type is Number.
    In BRF+, I call another global function to calculate the value, I define a local variant LV_VALUE to get export value from this function, this local variant's type is Amount, now I need to move this local variant value to the export field 'AMOUNT'.
    But BRF+ doesn't allow to convert amount data element to number data element directly, and I find there is no formula function available. Only there is a function 'TO_AMOUNT' to convert number data element to amount data element.
    Please help to get a solution for above solution.
    Thanks.

    Hi,
    within the formula expression within the category "Mathematical functions" you will find the function "TONUMBER". This converts a data element of type currency to a number. According to the help of this function:
    "The following conversion rules apply:
    <Amount>: The number part is returned. The currency unit is omitted."
    This is what you are looking for.
    For the other way round you already mentioned the correct function "TO_AMOUNT"
    BR
    Christian

  • Subtract   two dates to get   months and days

    I havew to subtract two dates and get the differnce ,
              Date issuedDate= myobj.getIssueDate();
              Date expirationDate=DateUtils.addMonths(issuedDate, 6);
              long timeDiff=expirationDate.getTime()-new Date().getTime();
              long daysRemaning=timeDiff/86400000;this is code I get the number of days between the issue date and todays date.now my client wants not just days but months and days , i need suggestions on how to get days and months between any two given dates

    I have read that it is best to use java.util.Calendar for almost any time related issue.
    With some switch statements and a for loop, I think the Calendar class could solve the problem.

  • Getting year, month and date

    I am getting the current year, month and date as follows:
    public void setDate(Date date) {
    String strDate = "";
    //year
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    strDate += cal.get(Calendar.YEAR);
    //month
    strDate += cal.get(Calendar.MONTH) + 1;
    //date
    strDate += cal.get(Calendar.DATE);
    this.date = strDate;
    }This gives me the date in teh format yyyymd.
    i.e for Mar 5 2007, it returns 200735
    How can it retrieve it in yyyymmdd format?
    i.e. Mar 5 2007 must be 20070305

    use the function:
    private String getTwoNumberFormat(int i){
         if (i<10)
              return "0"+i;
         else
              return ""+i;
    }and then call
    //month
    strDate += getTwoNumberFormat(cal.get(Calendar.MONTH)+1);or use SimpleDateFormat :)

Maybe you are looking for