Subtracting days from GregorianCalendar

Hi there,
How does one subtract, let's say, 7 days from a given GregorianCalendar instance. Let's say I want to set up two instances on GregorianCalendar. The first one has a known date, like so:
int endYear = 2005;
int endMonth = 7;
int endDay = 6;
GregorianCalendar endDate = new GregorianCalendar();
endDate.set(endYear,endMonth,endDay);
How do I set up a second instance of GregorianCalendar set to 7 days prior to the endDate instance?
GregorianCalendar nearDate = new GregorianCalendar();
What I want to do here is make a comparison with todays date (a third instance of GregorianCalendar that is set to todays date), to see if todays date is within the range between endDate and nearDate.
Can someone please help?
Alan

Calendar today = new GregorianCalendar();
Calendar nextWeek;
today.setTimeInMillis(System.currentTimeMillis());
nextWeek = new GregorianCalendar();
nextWeek.setTimeInMillis(today.getTimeInMillis());
nextWeek.add(Calendar.DATE, 7);
if (otherCalendar.getTimeInMillis() >= today.getTimeInMIlis() && otherCalendar.getTimeInMillis() <= nextWeek.getTimeInMillis()) {
    // otherCalendar is between now and a week from now
} Of course, the above tests for a 7 day stretch starting at whatever date and time it is now and ending at the same time a week from now. If you really want "any time today (Saturday), tomorrow, etc. through next Friday" then you'll need to adjust the hours, minutes, seconds, and millis to zero.

Similar Messages

  • Trying to subtract days from a date

    Im trying to subtract days from a date.
    When i use this query:
    select sysdate-:p21_DAYS_OLD from dual;
    it displays the correct date but not in the correct format. It is displaying 12-AUG-09 instead of 08/12/2009.
    I tried this query but i get the ORA-01722: invalid number error.
    select to_char(sysdate,'MM/DD/YYYY')-(:p21_DAYS_OLD) from dual;
    Can someone help me please?
    Deanna

    Dclipse03 wrote:
    Im trying to subtract days from a date.
    When i use this query:
    select sysdate-:p21_DAYS_OLD from dual;Just set the NLS_DATE_FORMAT parameter for your session and execute the above query.
    ALTER SESSION SET NLS_DATE_FORMAT='MM/DD/YYYY'
    /

  • Subtracting Days from a Date

    Am using Acrobat Pro 9 for Windows.  I have a form where I want to subtract days from Date1 and the result is Date2.  The Date1 and Date 2 text boxes are unformatted.  I've tried reading the "date" articles and experimental with some formulas but I'm afraid I'm still Acrobat illiterate.  Any ideas?  Thanks.

    It would be best if you set up the Date1 field to be formatted as a date. That way, Acrobat uses a built-in routine to automatically validate that the entry represents a valid date. You can then use a custom Calculate script for the Date2 field that looks someting like:
    // Custom Calculate script for text field
    (function () {
        // Get Date1 field value
        var v = getField("Date1").value;
        // If field is empty, blank this field
        if (v === "") {
            event.value = v;
            return;
        // Convert the date string to a date object
        var d = util.scand("mm/dd/yyyy", v);
        // Subtract 5 days
        d.setDate(d.getDate() - 5);
        // Set this field's value
        event.value = util.printd("mm/dd/yyyy", d);
    Change the date format (mm/dd/yyyy) in the code above to match what you want to use.

  • How to subtract days from day

    Hi,
    how to subtract 18 days from day : 04-10-2004
    rgds,
    Karna

    SQL> select to_date('04-10-2004','dd-mm-yyyy')-18 from dual
    SQL> /
    TO_DATE('0
    16/09/2004
    SQL>

  • Subtract days from given date

    hi,
    Is there any FM which will subtract the no of days from a given date.
    I hv tried with SUBTRACT_TIME_FROM_DATE , but having some config issue. And it is not available in 3.1H version.
    Some one can help me out...
    Thanks,
    shiva

    Hi,
    use the FM RP_CALC_DATE_IN_INTERVAL
    and check the sample output.
    Import parameters               Value             
    DATE                            10/10/2004        
    DAYS                            05                
    MONTHS                          00                
    SIGNUM                          +                 
    YEARS                           00                                                                               
    Export parameters               Value             
    CALC_DATE                       10/15/2004  
    Regards
    vijay

  • How to subtract a day from the presentation variable @{system.currentTime}

    Hello,
    How can subtract a day from the presentation variable - @{system.currentTime}
    I use the above as a title in the report. However I want to subtract a day from the above variable. How can I accomplish it?
    Thanks.

    Hi,
    Did you try the steps which I mentioned above. You can use TIMESTAMPADD function in many places in your report but I think not in title section of the title view.
    Also you can try using Narrative view instead of title view to achieve this. Follow below steos:
    * Pull another column in the report and change its fx to TIMESTAMPADD(SQL_TSI_DAY, -1, @{system.currentTime})
    * Now go to the Narrative view and add the below text in Narrative section of the view.
    Active Person Report as of @1
    Here @1 assuming the new column created is placed before all the columns in the request.
    * Also set the 'Rows to Display' as 1.
    * Delete Title view from the report and this narrative view on top of your report.
    Hope this helps.
    Thanks

  • Reg : subtracting weekoff day from the time difference

    Hi ,
    I have query where I am getting time difference first of all in terms of hours as:
    DECLARE @L_TIME_DIFF   INT
    SET @L_TIME_DIFF= (DATEDIFF(hh, '05/09/2014', '05/12/2014'))
    i.e. subtracting 12th may - 9th may and getting diff in hours ..in this case it will be 3*24 = 72 hours
    All fine till here. Now next step I need to subtract from this 72 hours if falling any week off day. It can be Friday, it can be Saturday, or Sunday, or Half day Saturday, or Half Day Sunday, or Full 2 days Saturday/Sunday  .
    The above scenario is decided based on one table [CALENDER_WORKINGDAYS] which looks like below :
    CAL_WORKING_DAY 
    CAL_AM
    CAL_PM
    CAL_SEQ_NO
    Monday
    1
    1
    1
    Tuesday
    1
    1
    2
    Wednesday
    1
    1
    3
    Thursday
    1
    1
    4
    Friday
    0
    0
    5
    Saturday
    1
    1
    6
    Sunday
    1
    1
    7
    So in this case, if you look at friday (CAL_AM=0 and CAL_PM=0) this measn friday full day holiday for some country.
    Hence I need to : 72 - 24 = 48 Hours should be my output.
    In another scenario, let s say some country work half day Saturday and Sunday full day off.
    So my output should be : 72 - 36 (because 24 hours for sunday off, and 12 hours for saturday half day off) =
    36 should be my output
    and so on..
    Can you help to build such query based on the above table given ?
    Thanks

    Hi Jose Diz,
    I am trying the below logic. Seems it should be working:
    DECLARE
    @L_START_TIME    DATETIME,          
    @L_END_TIME    DATETIME,
    @L_TIME_DIFF   INT, -- Time Difference considering week offs          
    @W_TIME_DIFF   INT ,-- Whole Time Difference,
    @PUBLICHOLS   INT,
    @WEEK_OFF INT
    set @L_START_TIME    = '5/9/2014';
    set @L_END_TIME    = '5/12/2014';
    /* TO GET THE TIMDIFFERENCE */          
      SET @L_TIME_DIFF=(DATEDIFF(hh,@L_START_TIME,@L_END_TIME))          
      SET @W_TIME_DIFF = @L_TIME_DIFF          
    -- Subtracting day off in the week(i.e. sat/sun or in some case friday) if it is in between start and end
      set @WEEK_OFF= 0;
        SELECT @WEEK_OFF= @WEEK_OFF + 12 * (abs(CAL_AM -1) + abs(CAL_PM -1))
        from CALENDER_WORKINGDAYS
     IF DATENAME(WK,@L_END_TIME) > DATENAME(WK,@L_START_TIME)           
     BEGIN          
      SET @L_TIME_DIFF= @L_TIME_DIFF - @WEEK_OFF          
     END          
    Thanks to confirm

  • Subtract business days from date - calculated column

    Hello,
    I had a calculated column on a library that took two dates and found the difference between them in business days, but I am not sure how to subtract business days from a date...for instance I get a start date from a form and I need to
    subtract 10 business days from that date.
    Can anyone help?

    I've always resorted to Javascript/JQuery for that kind of function. I found an old fashioned loop worked the best for me - it supports going forward or backwards. I key it off of a change in a starting date, or sometimes a status change. My actual production
    code takes into account another list where we remove holidays and non-work days.
    newDate = getNextDate(newDate, -3);
    $("input[title='Date Due']").val((newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + newDate.getFullYear());
    function getNextDate(currentDate, offset) {
    // offset is business days
    var wkend = 0;
    var index = Math.abs(offset); // need positive number for looping
    var neg = true;
    if(offset >= 0) { neg = false; }
    var curDOW = currentDate.getDay();
    var nextDate = new Date(currentDate);
    for(var i=1; i <= index; i++) {
    nextDate.setDate(nextDate.getDate() + (neg ? -1: 1));
    var nextDOW = nextDate.getDay();
    if(nextDOW == 0) {nextDate.setDate(nextDate.getDate() + (neg ? -2: 1));} // Sunday
    if(nextDOW == 6) {nextDate.setDate(nextDate.getDate() + (neg ? -1: 2)); } // Sat
    // alert("offset is " + offset + "start: " + currentDate + ", next date is " + nextDate);
    return nextDate;
    Robin

  • Need to subtract one day from the date appearing

    Hi, I am modifying a script in such a way that for an output type there is an invoice date appearing.  This invoice date should be back dated to 1 date less to the actually date that is appearing currently.
    For ex the date is 2007.08.30 it should appear as 2007.08.29.
    While debugging I found that it is picking up the data for the date from the structure vbdkr and the field is fkdat. From the following code in the script.
    /:   DEFINE &SALES_ORDER& := &VBDKR-VBELN_VAUF&
    /:   INVOICE DATE,, : &VBDKR-FKDAT&
    What changes do I need to make the changes in the script or in the driver program?
    Can you please suggest?
    Thanks.

    Hi..
    You can get this Functionality by Calling a FORM (subroutine) from the Script Layout itself  There is no need to change the Print program.
    Eg:
    In the layout set -> window -> text elements. Write this before displaying invoice date
    /: PERFORM F_DATE_SUB IN PROGRAM ZPRG01
    /: CHANGING &VBDKR-FKDAT&
    /: ENDPERFORM
    INVOICE DATE,, : &VBDKR-FKDAT&
    Create the report program ZPRG01: In the program ZPRG01
    FORM F_DATE_SUB TABLES INTAB STRUCTURE ITCSY
                                                  OUTTAB STRUCTURE ITCSY.
    DATA: L_DATE TYPE D.
    READ TABLE OUTTAB INDEX 1.
    L_DATE = OUTTAB-VALUE. "you need to convert here
    SUBTRACT  1 FROM L_DATE.
    OUTTAB-VALUE = L_DATE.
    MODIFY OUTTAB INDEX 1.
    ENDFORM.
    reward if Helpful.

  • Get days from the java.util.date class

    Does anyone know how to get the days from the java.util.date
    class. I'm trying to subtract two dates to get the total days between
    the two dates. Any help would be appriciated
    Rob

    If you use the getTime() method, you get the date as a number of milliseconds since a predefined time. You can do arithmetic on that number, such as subtracting two of them to get the number of milliseconds between two Dates, and so on.

  • How to subtract time from dateTime in xquery?

    How to subtract time from dateTime in xquery??
    In ALDSP i tried to use the function xf:add-days but it is showing an error saying invalid function name. I think this namespace is not valid for ALDSP. Is there is any way to add this function in ALDSP???

    xquery functions and operators documentation is here :
    http://www.w3.org/TR/xpath-functions/
    It sounds like you want to ..
    op:subtract-dayTimeDuration-from-dateTime
    this is not an xquery function that you can call, it is an xquery operation defined by the specification
    I believe you can simply use the '-' operator to use that operation.
    <newDateTime>
    { $myDateTime - $myTime }
    </newDateTime>

  • Subtracting sysdate from parameter

    All,
    I need to have user input parameter value.
    for example i want to restrict query based on input parameter.
    I have created a paramter as old_report where user would enter number of days they want to go back and see reports for.
    So my where condition is
    Receipt_date <=TRUNC(SYDATE)- :Old_report.
    whenever i tried to enter value it fails with error wrong value '5' for paramter
    Looking at documents it says that it requires same datatype with reserved words.
    SYSDATE being reserverd words how do i convert numeric values to date format?
    Any one please help.
    Thanks

    Hi
    This should work, you need to convert the value received from the parameter into a number. By default all parameters associated with <NONE> are strings.
    Let's assume that receipt_date is a date and that it is truncated, then this algorithm, receipt_date <= TRUNC(SYSDATE) will return all receipts.
    Taking your example, let's say we have a parameter called START_DAYS and the user keys 5. This alogrithm, receipt_date <= (TRUNC(SYSDATE) - to_number(START_DAYS)) will only return receipts that have dates five days in the past.
    You need to teach your users to only key numbers otherwise this will fail because to_number('A') will not compute. Of course you ought to trap NULL values and negative numbers too, which the following will do. We trap negative numbers so that dates don't end up in the future. This algorithm uses zero for NULL and negative values, essentially subtracting nothing from the system date.
    RECEIPT_DATE <= TRUNC(SYSDATE) - DECODE(SIGN(TO_NUMBER(NVL(START_DAYS,0))),-1,0,TO_NUMBER(NVL(START_DAYS,0)))
    Hope this helps
    Regards
    Michael

  • How to subtract Time from Sysdate

    Hello,
    I want to subtract time from sysdate and get the sysdate with time.
    Example : = (Sysdate with time)03-mar-2002 16:40:55 - 8 (hours). The result should be like :03-mar-2002 08:40:55
    How to write a query for this?.
    Please let me know as soon as possible.
    Thanks,
    Ravi.

    Hi Ravi...
    SYSDATE includes time, TRUNC(SYSDATE) does not include time.
    If you subtract these two, you'll get a positive number of 1.00 or less which is the fraction of a day. For instance high noon would have a fraction of 0.50000. 12:01 p.m. would be 0.50000 + (1/(24*60)), or 0.50069444....
    If you want to set the clock back, by calculating a "date" that is 8 hours less than the SYSDATE, then SYSDATE - (8/24) will do that. This will work even if SYSDATE was something like 2 a.m., because it will then set it to 6 p.m. of the day before the day in SYSDATE.
    If you want to set a "date" to a specific time on the day of SYSDATE, then calculate the fraction for that time and add it to the TRUNC(SYSDATE). High noon would be TRUNC(SYSDATE) + 0.5.
    SELECT TO_CHAR(TRUNC(SYSDATE)+0.5,'DD-MON-YY HH:MI:SS am') AS NOON FROM DUAL;Hope this helps...
    Larry Johnson

  • Subtract Time  from Time

    Dear Sir,
    I want to subtract Time from time, Please help me
    SELECT A.EMP_CODE,
    TO_DATE(A.INTIME,'DD/MM/RR')IN_DATE
    ,MIN(TO_CHAR(A.INTIME,'HH24:MI')) IN_TIME
    ,MAX(TO_CHAR(A.OUTTIME,'HH24:MI')) OUT_TIME
    ,TRUNC(SUM(A.TDAYS)*24)||':'||TRUNC((SUM(A.TDAYS)*24-TRUNC(SUM(A.TDAYS)*24))*60) TOTAL_TIME
    FROM HCM.EINOUT A, HCM.PERSONNEL B
         WHERE A.EMP_CODE = B.EMP_CODE AND
         TO_DATE(A.INTIME,'DD/MM/RR')BETWEEN
         TO_DATE('01/08/12','DD/MM/RR') AND TO_DATE('15/08/12','DD/MM/RR')
         AND B.DEPT_CODE = 16 and a.emp_Code = '09833'
         GROUP BY A.EMP_CODE, TO_DATE(A.INTIME,'DD/MM/RR')--, a.tdays
         ORDER BY 1, 2 ASC;
    I want to subtract 20:00 from (out_time) column from every record
    If I subtract it from first record then result in over_time column will be 2:46
    (out_time - 20:00)
    --------------Out put........................
    code date in_time out_time total_time over_time
    09833     02-AUG-12     09:18     22:46     13:28
    09833     03-AUG-12     09:16     21:50     12:34
    09833     04-AUG-12     09:28     21:41     12:13
    thanks

    this will help
    --And if difference can be > 24 hours:
    WITH t AS
         (SELECT TO_DATE ('26-aug-2011 10:10:10',
                          'dd-mon-yyyy hh24:mi:ss'
                         ) start_date_time,
                 TO_DATE ('26-aug-2011 10:12:10',
                          'dd-mon-yyyy hh24:mi:ss'
                         ) end_date_time
            FROM DUAL)
    SELECT    TRUNC (end_date_time - start_date_time)
           || ' days '
           || TO_CHAR (TRUNC (SYSDATE) + (end_date_time - start_date_time),
                       'hh24:mi:ss'
                      ) time_diff
      FROM t
    --Or interval based solution for < 24 hour diff:
    WITH t AS
         (SELECT TO_DATE ('26-aug-2011 10:10:10',
                          'dd-mon-yyyy hh24:mi:ss'
                         ) start_date_time,
                 TO_DATE ('26-aug-2011 10:12:10',
                          'dd-mon-yyyy hh24:mi:ss'
                         ) end_date_time
            FROM DUAL)
    SELECT REGEXP_SUBSTR (NUMTODSINTERVAL (end_date_time - start_date_time, 'day'),
                          '\d\d:\d\d:\d\d'
                         ) time_diff
      FROM t
    --interval based solution for >= 24 hour diff:
    WITH t AS
         (SELECT TO_DATE ('26-aug-2011 10:10:10',
                          'dd-mon-yyyy hh24:mi:ss'
                         ) start_date_time,
                 TO_DATE ('26-aug-2011 10:12:10',
                          'dd-mon-yyyy hh24:mi:ss'
                         ) end_date_time
            FROM DUAL)
    SELECT REGEXP_REPLACE (NUMTODSINTERVAL (end_date_time - start_date_time,
                                            'day'),
                           '^.0*(\d+) (.{8}).+$',
                           '\1 days \2'
                          ) time_diff
      FROM t
    --another method
    SELECT    TO_CHAR (TRUNC (nb_sec / 3600), '00')
           || ':'
           || TO_CHAR (nb_min - TRUNC (nb_sec / 3600) * 60, '00')
           || ':'
           || TO_CHAR (MOD (nb_sec, 60), '00')
      FROM (SELECT (end_date_time - start_date_time) * 24 * 3600 nb_sec,
                   (end_date_time - start_date_time) * 24 * 60 nb_min
              FROM ps_tb_ln_pdc_status_log)
    --another method
    SELECT SYSDATE + 3.45 / 24 AS "End Date", SYSDATE AS "Start Date",
           CAST (SYSDATE + 3.45 / 24 AS TIMESTAMP) - SYSDATE AS "Duration"
      FROM DUAL;Regards,
    friend.

  • Can I create a field that calculates number of days from a date field?

    Hi all,
    I need a field that will calculate the number of days elapsed whatever date is entered into the date field, and update that number of days based on the calendar/computer calendar date.  Does that make sense?
    https://www.dropbox.com/s/arkmnsxjkl4r156/AFBS_FacilitySpecialCareList02.pdf
    I set up the form so that my boss can add or subtract clients from the list.  Each client needs to have its own "Days since added to the list" number. Sorta like a little aging report.
    Is this possible?
    Many thanks,
    Laura

    Wow, I'm so stumped.  I tried to copy a bit of formcalc into the exit event for my current date field to try to make this happen, but I can't get it to work.
    My  attempt at scripting is embarrassing, but here it is:
    form1.sf1.CCSub.CCGroup.#subform[0].DateField2::exit - (FormCalc, client)
    form1.sf1.CCSub.CCGroup.#subform[0].NumericField2.rawValue = Date2Num(DateField1, "MM-DD-YY") - Date2Num(DateField2, "MM-DD-YY")
    and here's the file:
    https://www.dropbox.com/s/arkmnsxjkl4r156/AFBS_FacilitySpecialCareList02.pdf
    Can anyone help me get this right?
    Sign me: One Lost Designer
    Thanks!

Maybe you are looking for