Java code to get the difference between two dates in days

Hi ppl,
I need to write a user defined function to get the difference between two date nodes, in days.Please help me out
regards,
Prashanth

Hi,
have a look at those two:
(How do I calculate the difference between two dates?)
http://joda-time.sourceforge.net/faq.html
Calculating the Difference Between Two Datetime Stamps
http://www.xmission.com/~goodhill/dates/deltaDates.html
Get difference in days
http://javaalmanac.com/egs/java.util/CompDates.html
Regards,
michal

Similar Messages

  • How to get the difference between two date

    Hello,
    I want to know how to write a code the tell me the difference between two date, I am using
    oracle.jbo.domain.Date
    i have a rent date and return date so my code is
    Date rent=(Date)nr.getAttrbute("RentDate"),ret=(Date)nr.getAttrbute("ReturnDate");
    is there a way to know the difference in days between those two dates ?
    Thanks

    hi,
    try this.....
    DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
    Date date = (Date)formatter.parse(dateStr); //// dateStr <- from date value (that is string value)
    Date dateto = (Date)formatter.parse(datetostr); //// datetostr <- to date value (to date getting from as a string)
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    Calendar calto = Calendar.getInstance();
    calto.setTime(dateto);
    fromDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
    toDate = calto.get(Calendar.DATE) + "/" + (calto.get(Calendar.MONTH) + 1) + "/" + calto.get(Calendar.YEAR);
    // System.out.println("from Date : " + fromDate);
    if ((fromDate != null && toDate != null) && (date.compareTo(dateto) > -1) ) {                  
    fc.addMessage("VacationQueryComponent", new FacesMessage(FacesMessage.SEVERITY_ERROR, "From Date cannot be lower than To Date", "From Date cannot be lower than To Date"));
    fc.renderResponse();
    thks.

  • How to get the diff between two dates  -  # of days

    Hi Experts,
    I have requirement to know the difference between dates. In my presentation layer two columns as created date and updated date. I want to know the difference in # of days in the presentation services.
    Please provide some pointers.
    Thanks in adv
    svr

    Hi,
    In the presentation layer you can create a column with a custom formula of this:
    TIMESTAMPDIFF(SQL_TSI_DAY, datecolumn1,datecolumn2)
    The result shown in the column will be the difference in days between the two dates.
    Regards,
    Matt

  • How to get the difference between two dates???

    HI I have two dates and I want to test if there is an hour between them.
    Is there a way to compare two dates like mySQL can with dateDiff(date1,date2);
    Thanks alot.
    Graham

    This link is now out of date as they have revamped the structure. Yep. Here it is again: http://forum.java.sun.com/thread.jsp?forum=31&thread=502686
    I've also noticed that my comment regarding daylight saving was misleading. The demonstrated method for calculating difference between Dates is reliable. However, one needs to be careful when interpreting Dates in a daylight saving timezone.

  • How to get the difference between two columns in a column group

    Hi All,
    My first time here and really new to programming. I would like to get the difference between 2 columns that are inside 
    a column group.
    Here is my sample table below: The Column Group is PeriodNumber and can only choose 2. like 1 and 2.. I would like to have a third row which will simply calculate the difference between the amounts in PeriodNumber 1 and 2.
                                PeriodNumber          
    Account                    1                            2     
    1) Cash                10,000                15,000
    2) Receivables      12,000                11,500
    3) Equipment          5,000                  5,500
    Total Assets          27,000                32,000

    Hi yabgestopa,
    From your description, you want to get the difference between two columns in a column group. After testing it in my environment, we can use custom code to achieve your requirement. For more details, you can refer to the following steps:
    Copy the custom code below and paste it to your report. (Right-click report>Report Properties>Code)
    Dim Shared Num1 As Integer
    Dim shared Num2 As Integer
    Public Function GetAmount(Amount as Integer, Type as String)
    If Type = "1" Then
    Num1=Amount
    Else
    Num2=Amount
    End If
    Return Amount
    End Function
    Public Function GetDif()
    Return Num1-Num2
    End function
    Right-click the second column to insert a third column with Outside Group-Right.
    Then use the expressions below in the matrix.
    =Code.GetAmount(Fields!Amount.Value,Fields!PeriodNumber.Value)
    =code.GetAmount(Sum(Fields!Amount.Value),Fields!PeriodNumber.Value)
    =Code.GetDif()
    The report looks like below.
    If you have any questions, please feel free to ask.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • Function Module to get the difference between two times.

    Hi All,
    I want to know if there is any function module that gives me the difference between two specified times.
    For Ex: Time 1: 12/01/2007 00:01 A.M
                Time 2: Time 1 - 180 Seconds. This changes the time, also may even change the Date. (As in above situation, the Time 2 will be 11/30/2007 11:58 P.M)
    Could some one please suggest if there is any function module for this case.
    Thanks in Advance.

    Hi, You can use the following function module to calculate the diffrence between to dates in the unit that you want.
    COPF_DETERMINE_DURATION calculates the difference between two dates and time in minutes and hours.
    Parameters:
    EXPORTING
         I_START_DATE "The start date of the time interval
         I_START_TIME "The start time of the time interval
         I_END_DATE   "The end date of the time interval
         I_END_TIME   "The end time of the time interval
         I_UNIT_OF_DURATION "Time unit of the duration if not to be calculated in days.
         Value     =====     Meaning
         ' '     =====     Day (default)
         D     =====     Days
         H     =====     Hours
         MIN     =====     Minutes
         MON     =====     Months
         S     =====     Seconds
         WK     =====     Weeks
         YR     =====     Years
         I_FACTORY_CALENDER     "A factory calender if not using the Gregorian calender
    IMPORTING
         E_DURATION      "Time difference in unit specified.
    Message was edited by:
            Rajesh Soman

  • Getting the difference between 2 dates

    Hi,
    I am creating a Date object (using today;s day, month and year. hours and minutes come from a config file) and getting the difference between this date and the current date and time.
    My code is as follows:
    I am passing 07:30 into the method as the parameter aSstart_time.
    Calendar newCalendar = Calendar.getInstance(Locale.UK);
        SimpleDateFormat dtformat = new SimpleDateFormat("dd-MM-yy HH:mm");
        int lIYear = newCalendar.get(newCalendar.YEAR);
        int lIMonth = newCalendar.get(newCalendar.MONTH);
        int lIDay = newCalendar.get(newCalendar.DAY_OF_MONTH);
        String lSstartHrs = aSstart_time.substring(0,aSstart_time.indexOf(":"));
        //System.out.println(">>>>>>>>> THE HOURS IS: " + lSstartHrs);
        String lSstartMins = aSstart_time.substring(aSstart_time.indexOf(":")+1,aSstart_time.length());
        //System.out.println(">>>>>>>>> THE MINS IS: " + lSstartMins);
        java.util.Date ldStartTime = new java.util.Date(lIYear,lIMonth,lIDay, Integer.parseInt(lSstartHrs),Integer.parseInt(lSstartMins));
        System.out.println("Year: " + lIYear);
        System.out.println("Month: " + lIMonth);
        System.out.println("Day: " + lIDay);
        System.out.println("Hour: " + Integer.parseInt(lSstartHrs));
        System.out.println("Min: " + Integer.parseInt(lSstartMins));
        System.out.println("The number of milliseconds for current time is: " + newCalendar.getTime().getTime());
        System.out.println("The number of milliseconds for 07:30 is: " + ldStartTime.getTime());
        System.out.println("calendar time: " + dtformat.format(newCalendar.getTime()).toString());
        System.out.println("created time: " + dtformat.format(ldStartTime).toString());
        if(newCalendar.getTime().getTime() < ldStartTime.getTime()){
          System.out.println("The current time is less than the config time (07:30)");
        else{
          System.out.println("The time is greater than 07:30");
        }When I run this program at 10:06am I get the following output:
    Year: 2006
    Month: 1
    Day: 15
    Hour: 7
    Min: 30
    The number of milliseconds for current time is: 1139997962321
    The number of milliseconds for 07:30 is: 61098132600000
    calendar time: 15-02-06 10:06
    created time: 15-02-06 07:30
    The current time is less than the config time (07:30)
    The Date.gettime() method should measure the number of milliseconds after 01-01-1970 so the number of milliseconds for the current time (10:06am) should be greater than the number of milliseconds for 07:30. A you can see in the above output this is not the case.
    Anybody have any idea why this is happening. Am I using the .gettime() method for the correct purpose.
    Any ideas or suggestions would be greatly appreciated.
    Thanks

    It is beacause Calendar use fully defined year as parameter, so Calendar.set(year + 1900, month, date, hrs, min) is (almost) equivalent to new Date(year, month, date, hrs, min)Example:    int year = 85; // old way of specifying a year in a Date
        int month = 3;
        int date = 12;
        int hrs = 12;
        int min = 0;
        Calendar cal = Calendar.getInstance();
        cal.set(year + 1900, month, date, hrs, min);
        Date d = new Date(year, month, date, hrs, min);
        System.out.println(d);
        System.out.println(cal.getTime());You will obtain the same date (except seconds and miliseconds in this case, as they probably won't be zero with Calendar.getInstance(); )

  • Calculate the difference between two dates times in infopath form 2013

    Hi,
    I have an infopath 2013 form that contains three fields:
    2 date time and the 3rd contains the difference between the two in hours
    how I can make the difference between the two so that the display will be like this:
    Date Time1           08/21/2014           22:00 
    Date Time2           08/22/2014           1:00
    Diff Field                                             3:00

    Hi,
    Please refer to the following article which matches your requirement exactly.
    Calculate the difference between two date picker controls in InfoPath using rules and formulas - no code!
    Please mark it answered, if your problem resolved.

  • As to the data type of the data type of the difference between two date type of datas

    Hi,
    I have a question about the data type of the difference between two date type of datas.
    There are two date type of datas as:
    SSHIPMENTS.RECEIVEDATETIME
    SSHIPMENTS.PROMISEDATETIME
    I try to use the following SQL Script in Oracle SQL*Plus as:
    SELECT CASE
    WHEN (SSHIPMENTS.RECEIVEDATETIME - SSHIPMENTS.PROMISEDATETIME) < '000 01:00:00.000' THEN 'OnTime'
    WHEN (SSHIPMENTS.RECEIVEDATETIME - SSHIPMENTS.PROMISEDATETIME) < '000 01:30:00.000' THEN '60-89 Minutes'
    ELSE '3+ Hours'
    END
    FROM SSHIPMENTS;
    The error message of "Invalid Number" for the '000 01:30:00.000' happens.
    I don't know if the data type of the interval is wrong.
    Many Thanks,
    Cathy

    SELECT CASE
    WHEN (to_char(SSHIPMENTS.RECEIVEDATETIME,'hhmiss') - to_char(SSHIPMENTS.PROMISEDATETIME,'hh24miss')) < '010000' THEN 'OnTime'
    WHEN (to_char(SSHIPMENTS.RECEIVEDATETIME,'hhmiss') - to_char(SSHIPMENTS.PROMISEDATETIME,'hh24miss'))< '000 01:30:00.000' THEN '60-89 Minutes'
    ELSE '3+ Hours'
    END
    FROM SSHIPMENTS;
    just try it out..

  • 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.

  • Calculate the Difference Between two dates excluding weekends and Holidays

    Hi,
    We need to calculate the difference between the two dates by excluding the Local public holidays (It is global and varies across countries) and weekends should not be included in calculation for a business day in OBIEE.
    We have two dates: Open date and close date when ever close date is null we are calculating age based on taking the current timestamp and need to exclude the weekends and Holidays when ever the close date is null.
    Ex:
    Col1 col2 Total
    11/9/2010 2:46:38 PM Null 13
    11/2/2010 8:06:26 PM 11/3/2010 5:37:03 PM 1
    (In the Total we shouldn't include the weekends,holidays)
    Please let me know how to calculate the difference between two dates by excluding the weekends and holidays.
    Thanks
    Edited by: user10441472 on Nov 22, 2010 3:14 PM

    You already asked this question and I answered it...
    Re: calculation of Business day in OBIEE

  • Calculate the difference between two dates

    I would like to calculate the difference between two dates in PL/SQL and return the result as a number of days expressed as an integer value.

    Denes,
    A fair point, I should really have posted this on the SQL forum (I'm new to the forum as well as PL/SQL) but thanks for responding anyway. It does raise a question as to how to implement this in ApEx though.
    I have created the function and am calling it as shown below from the source window of a form. The source type is 'PL/SQL expression or function' and the expression in the source window of the form is:
    calc_date_difference (:p26_c_payment, :p26_c_rec)
    The two parameters being passed are of type date but I'm not sure how to handle the ruturned number and populate the form in ApEx with this value.
    Is it possible to do it this way or am I taking completely the wrong approach?
    Regards
    Sandy
    This is not ApEx related but SQL related:
    CREATE OR REPLACE FUNCTION calc_date_difference (
    p_date_1   VARCHAR2,
    p_date_2   VARCHAR2
    RETURN NUMBER
    v_difference   NUMBER;
    v_sql_err      VARCHAR2 (4000);
    BEGIN
    v_difference := TRUNC (TO_DATE (p_date_1)) - TRUNC
    (TO_DATE (p_date_2));
    RETURN v_difference;
    CEPTION
    WHEN OTHERS
    THEN
    v_sql_err := SQLERRM || CHR (10) || CHR (10) ||
    SQLCODE;
    ND calc_date_difference;and
    SQL> SELECT calc_date_difference ('23.01.2007',
    '20.01.2007') diff
    2    FROM DUAL;
    DIFF
    3
    Denes Kubicek                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Trying to Get the "Difference" Between Two Tracks

    What I am trying to accomplish is this: I have an audio track of a voiceover with a music bed beneath it. I also have the bed music by itsself, without the voiceover. I want only the voice portion. Is there a way that I can "cancel out" the music portion or get the difference between the two tracks (just the voice portion)? It seems that there should be some means of doing this, but for the life of me I cannot figure out how to do it!
    Any advice?

    If it's exactly the same music track, then as long as you line it up sample-accurate and the levels co-incide, you can do a simple subtractive process. You need both files available, trimmed to length. Open them both in Edit View, and copy one of them to the clipboard. Open the other, and select Mix-paste, and use the 'invert' option. That will effectively subtract one from the other, with the difference being your vocal.
    Have to tell you though that the chances of getting this spot on are amazingly low... any errors at all leave you with bleed-through, about which you can do nothing.

  • How to get the difference between two HashMaps

    i have two HashMaps
    HashMap first = new HashMap<Integer,Object>();
    HashMap second = new HashMap<Integer,Object>();
    How to get the difference between one and two and put the result in the third
    HashMap third = new HashMap<Integer,Object>();

    My bad, difference of course means removeAll():Map<Integer,Object> first = new HashMap<Integer,Object>();
    Map<Integer,Object> second = new HashMap<Integer,Object>();
    Map<Integer,Object> third = new HashMap<Integer, Object>(first);
    third.keySet().removeAll(second.keySet());No loops needed.

  • Need to get the difference between two period

    Hi All,
    I need to get the difference of two posting period like period is in format YYYYMM.
    I need to subtract posting perid '200512' FROM '200601' how will be it possible.
    Please suggest.
    Thanks,
    Sandy

    Hi,
    YOu can also do like the following.
    Determine you first day of first period and last day of second period.
    data: first_day type datum,
          last_day type datum,
          days type i.
    first_day = date from FM RKE_GET_FIRST_DAY_IN_PERIOD  
    last_day = date from FM RKE_GET_LAST_DAY_IN_PERIOD   
    days = last_day-first_day.
    Thanks,
    Ramakrishna

Maybe you are looking for

  • Downgrading Firmware for the WRT54G... I can't do it...

    Hello, I am using the WRT54G wireless router, which currently has the 8.0.01 firmware. That's what it came with when i bought it. I am trying to downgrade the firmware to either 3.01.3 or 3.03.6. I want to downgrade to one of those because Xbox said

  • Downloading purchases from store unto my itunes

    Trying to download purchases unto my itunes for my ipad2, but give me an error "network connection timed out" any help?

  • FLICKR DISSAPEARED

    Under the PUBLISHING SERVICES SECTION in the Library Module Im missing my FLICKR module when I hit setup it does not let me add it any ideas?

  • About sharing files between 2 phones

    All the android has shareit(via wifi) app. By which they share files In short time.but we can't have it. But some times it installs.but doesn't work.so, what should we do. Plz Plz Plz help us. Is there any means that can reduce our problems by saving

  • Apps download & upgrade problem...

    Using a torch 9800 & recntly every time I try to download, upgrade a game or app it starts then stops saying, there was a problem during installation please try later. Have tried to restart but its no use, help!