Get date of first day of current week

HI!
Why does the following method do not return the date of the first day of the week in which the given date lies?
public static int getFirstDayOfWeek(int year, int month, int date) { 
GregorianCalendar GregCalendar = (GregorianCalendar)
new GregorianCalendar(year, month, date).getInstance();
while(GregCalendar.get(Calendar.DAY_OF_WEEK)!= Calendar.MONDAY) {
GregCalendar.roll(Calendar.DATE, false);
return GregCalendar.get(Calendar.DATE);     
What I want the method to do:
year = 2003
month = Calendar.JANUARY
date = 8
==> GregCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY
==> GregCalendar.roll(Calendar.DATE, false);
==> GregCalendar.get(Calendar.DATE) == 7 (?????????)
==> GregCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY
==> GregCalendar.roll(Calendar.DATE, false);
==> GregCalendar.get(Calendar.DATE) == 6
==> GregCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY
==> return GregCalendar.get(Calendar.DATE)
But in reality, it always returns 6 grrrrrrrr
What is wrong wirh it?
Yours RB

public Date getCurrentMonday()
        Date monday = null;
        Calendar rightNow = Calendar.getInstance();
        int day = rightNow.get(Calendar.DAY_OF_WEEK);
        int distance = 0;
        if (day == Calendar.MONDAY)
            monday = rightNow.getTime();
        else
            distance = day - Calendar.MONDAY;
            if (distance == -1)
                distance = 6;
            monday = (Date) (rightNow.getTime());
            monday.setTime(monday.getTime() - 1000 * 60 * 60 * 24 * (distance));
        return monday;
    }

Similar Messages

  • Date for first day of current month

    How can i get the date for first day of current month ?

    select trunc(sysdate,'MM'),to_char(trunc(sysdate,'MM'),'DD'),to_char(trunc(sysdate,'MM'),'Day') from dual;

  • How to get the date of first day of a week for a given date

    Hi gurus
    can any one say me how to get the date of first day(date of Sunday) of a week for a given date in a BW transformations. For example for 02/23/2012 in source i need to get 02/19/2012(Sunday`s date) date in the result. I can get that start date of a week using  BWSO_DATE_GET_FIRST_WEEKDAY function module. But this function module retrieves me the  start date as weeks monday(02/20/2012) date. But i need sundays(02/19/2012) date as the start date. So it would be really great if anyone sends me the solution.
    Thanks
    Rav

    Hi,
    The simplest way would be to subtract 1 from the date date which you are already getting in transformation routine, but instead of doing that subtraction manually which might need bit of errort, you can simply use another FM to subtract 1 from given date.
    RP_CALC_DATE_IN_INTERVAL
    Regards,
    Durgesh.

  • DATE FOR FIRST DAY OF THE WEEK

    I am new to Oracle and am looking for a procedure that will calculate the first day of the week given today's date.
    Given 1/23/2000 I need to find Sunday 1/21/2000.

    Hi there,
    I don't think there's a built-in Oracle procedure for what you want. However...
    In SQL (at least, Oracle's SQL), you have the function TO_CHAR and TO_DATE that allow you to convert a date to a string and vice-versa. They work pretty much in a similar way, that is :
    TO_<something>( <data_to_convert>, <format> )
    eg.: To have the day of the week for the current date, you can do
    SELECT TO_CHAR( SYSDATE, 'D' ) FROM DUAL;
    Wednesday, January 24th, 2001 would then return 4.
    To have the day of the year, you would do
    TO_CHAR( SYSDATE, 'DDD' )
    Then, to have the day of the year of the first day of the week, you then could subtract the day of the week of your date from the day of the year of your date, and then add one. To be able to do math stuff of char value, you must use TO_NUMBER.
    In one ugly line, you could do this by doing the following :
    select to_date( (to_char(sysdate,'YYYY') &#0124; &#0124; to_char( to_number( to_char(sysdate,'DDD')) - to_number( to_char(sysdate,'D')) + 1)),'YYYYDDD') from dual;
    Eurk.
    Or you could write a function that would return the date of the first day of the week of a specific date, like :
    create or replace
    FUNCTION FDOW (P_DATE DATE) RETURN DATE
    IS
    R_FDOW DATE;
    BEGIN
    R_FDOW := to_date( (to_char(P_DATE,'YYYY') &#0124; &#0124; to_char( to_number( to_char(P_DATE,'DDD')) - to_number( to_char(P_DATE,'D')) + 1)),'YYYYDDD');
    return R_FDOW;
    END;
    (FDOW : First Day Of Week - not really original, I know)
    Then, you can call it wherever you want:
    SELECT FDOW(SYSDATE) FROM DUAL;
    Have fun!
    Frederic Denis

  • How to get the date of first day of the week

    hi ,
    how can i get the first day of the current week ?
    i have tried the following
    note : my sysdate is 3rd april 2007
    select trunc(sysdate , 'ww') from dual -- 4/2/2007
    select trunc(sysdate , 'w') from dual -- 4/1/2007
    select trunc(sysdate - 4 , 'ww') from dual -- 3/26/2007 -- shld return 25th mar 2007
    select trunc(sysdate - 4 , 'w') from dual -- 3/29/2007 -- shld return 25th mar 2007pls advise
    tks & rgds

    Hi!
    I'm considering MONDAY as the first working day of the current week. And, the code is accordingly --
    SQL> select to_char(sysdate - to_number(decode(to_char(sysdate,'DY'),'TUE','1',
      2                               'WED','2',
      3                               'THU','3',
      4                               'FRI','4')),'dd-mon-yyyy') res
      5  from dual;
    RES
    02-apr-2007Regards.
    Satyaki De.

  • From where to get "First day of the week" data for all the locales, is it present in CLDR spec 24?

    I am trying to get "First day of the week" data from CLDR spec24 but cannot find where to look for it in the spec. I need this data to calculate numeric value of "LOCAL day of the week".
    This data to implement "c" and "cc" day formats that equals numeric local day of the week.
    e.g if "First day of the week" data for a locale is 2 (Monday) , it means numeric value for local day of the week will be 1 if it is Monday that day, 2 if it is Tuesday that day and likewise.

    Hi
    If you want to week to be started with Sunday then use the following formula:
    TimestampAdd(SQL_TSI_DAY, 1-DAYOFWEEK(Date'@{var_Date}'), Date'@{var_Date}') if it's retail week(starts from Monday) then the follow below:
    TimestampAdd(SQL_TSI_DAY, 1-DAYOFWEEK(Date'@{var_Date}'), Date'@{var_Date}')
    I'm assuming var_Date is the presentation variable for prompt...
    Edited by: Kishore Guggilla on Jan 3, 2011 4:48 PM

  • How to get first day of calendar week ?

    hi,
    i'm storing some calendar weeks in my database. for example 30/2013.
    How can i get the first day of this week (monday)? in this case it is 22.07.2013
    thx
    Anja

    As far as I know we don't have a function that converts week of a year into date. So you can try something like this.
    SQL> with t
      2  as
      3  (
      4  select '30/2013' week_year
      5    from dual
      6  )
      7  select min(dt) week_start_day
      8    from (
      9           select (level - 1) + to_date('01-01-' || substr(week_year, -4), 'dd-mm-yyyy') dt
    10                ,  week_year
    11             from t
    12          connect
    13               by level <=  to_date('31-12-' || substr(week_year, -4), 'dd-mm-yyyy') -
    14                            to_date('01-01-' || substr(week_year, -4), 'dd-mm-yyyy') +1
    15         )
    16   where to_number(to_char(dt, 'iw')) = to_number(substr(week_year, 1, instr(week_year, '/')-1));
    WEEK_STAR
    22-JUL-13

  • Date field input help - changing first day of the week

    Hi,
    I need to change the starting day of the week  in a date field value help. I also need to change the days name in this calender.
    Thanks in Advance,
    Dekel.

    I found how to change the first day of the week, through BAdi calendar_definition.
    Can anyone help me locate the day names? (I checked - FM day_names_get dosen't get the names of the UI search help).
    Thanks,
    Dekel.
    Edited by: dekel31 on May 29, 2011 1:17 PM

  • How to get the first day of current month

    hi guys,
    i am trying to get the first day of current month which get from the date i input at the selection screen. my method is not so good, so i was wondering if there is better way to get the this,
    thanks.

    Try this .
    data : DAYNR LIKE  HRVSCHED-DAYNR,
         DAYTXT LIKE  HRVSCHED-DAYTXT.
    data langu like sy-langu value 'EN'.
    Parameters PDATE LIKE SY-DATUM.
    PDATE+6(02) = '01'.
    CALL FUNCTION 'RH_GET_DATE_DAYNAME'
      EXPORTING
        LANGU                     = LANGU
        DATE                      = PDATE
      CALID                     =
    IMPORTING
       DAYNR                     = DAYNR
       DAYTXT                    = DAYTXT
      DAYFREE                   =
    EXCEPTIONS
      NO_LANGU                  = 1
      NO_DATE                   = 2
      NO_DAYTXT_FOR_LANGU       = 3
      INVALID_DATE              = 4
      OTHERS                    = 5
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    WRITE :/ PDATE, DAYNR, DAYTXT.
    Cheers

  • How to get the First day in a Week in a Fiscal Year

    Hi guys,
    I have a Requirement to calculate the First day in  a Week  in a Fiscal year .
    The FM GET*FIRST_DATE and all are for Calender year.
    Is there any FM to calculate the First in a Week in a Fiscal year.
    For eg. They will give 200826.
    In this 26 is the Week.
    I need to find the First date value in that Week.
    Thanks,
    Gokul

    Hi Gokul...
    Instead of using the funtion module you can just write the 3 statements of code to get the first day of the week.
    Its similar to the above one but instead of writing case statement you can achive the following.
    data : w_res type i,
             w_data type d,
    w_res = w_date mod 7.
    w_date = w_date - w_res.
    write w_date.
    This works.
    Regards,
    Siddarth

  • Get First day of a week

    Hi all,
    I have a question about using a presentation variable in a sql filter.Actually i have a presentation variable which return number of week per year ,the issue is that i want to have the firste day of the week selected in way to use it in the sql filter .
    Thanx for help

    Hi
    If you want to week to be started with Sunday then use the following formula:
    TimestampAdd(SQL_TSI_DAY, 1-DAYOFWEEK(Date'@{var_Date}'), Date'@{var_Date}') if it's retail week(starts from Monday) then the follow below:
    TimestampAdd(SQL_TSI_DAY, 1-DAYOFWEEK(Date'@{var_Date}'), Date'@{var_Date}')
    I'm assuming var_Date is the presentation variable for prompt...
    Edited by: Kishore Guggilla on Jan 3, 2011 4:48 PM

  • Getting first day of the week

    Dear All.
    As there is one function module BWSO_DATE_GET_FIRST_WEEKDAY which returns the first day of the week but its Monday but in our case we require Saturday and in some cases Monday.
    our user in UK are running the same report in which i have to calculate the stock base on starting day Monday and other uses are in KSA where the starting day of the week is Saturday.
    Is there any standard option available do achieve this.
    Kind regards,

    Hi Zeeshan,
    There are two options
    Option 1: In Routine or CMOD or in Variable use offset +5 or -2 according to your reporting requirements.
    Option 2: Maintain the table entries 52 Posting periods in T0009 or in custom table with special FISCAL Variant. (i.e., each week = one posting period). Have a loop up in that table according to your requirement.
    Hope either one of the option would help you in meeting your requirement.
    Best Wishes,
    BVC

  • Moving a GregorianCalendar to the first day of the week

    I'm using a GregorianCalendar class in order to get the time stamp for the nearest Sunday's date.
    here's what I'm trying to do:
    1. get a timeStamp and set it into a date
    Date startDate = new Date(startTime);2. create a new GregorianCalendar using the above date as the start time.
    GregorianCalendar calendar = new GregorianCalendar((startDate.getYear()+1900),startDate.getMonth(),startDate.getDate());3. the tricky part: make sure the calnedar's date is set to Sunday, if the current date is on any day other then Sunday, I will have to move it back to the nearest Sunday. I'm trying to do this by using:
    calendar.set(Calendar.DAY_OF_WEEK, 0);     for some reason this does not work...
    am I doing anything wrong here?

    thanks for that info but the problem is still there. here's some more input:
    //creating the date, the ts here is 1153858800000 which is the dated in 7/25/2006
    Date startDate = new Date(startTime);
    //creating the new calendar
    GregorianCalendar calendar = new GregorianCalendar((startDate.getYear()+1900),startDate.getMonth(),startDate.getDate());
    //setting the calendar to the first hour (not sure it necessary)
    calendar.set(Calendar.HOUR, 1);
    //setting the calendar to the first day of the week
    calendar.set(Calendar.DAY_OF_WEEK, 1);
    //this returns 1153778400000 which is still dated 7/25/2006 whic is a Tuesday I would expect it to returns a different date
    calendar.getTimeInMillis()     

  • Change first day of the week from Monday to Sunday(0CALWEEK)

    Hello!
    We have a report that display the data at a calendar week level. The start of the week currently show as Monday( retreived from FM - WEEK_GET_FIRST_DAY).
    Is there a global setting in source system to change the start day of the week from Monday to Sunday? I can always make this change in the transformation but would like to know if there is a global option to make this change so the rest of the reports display the same.
    Thanks
    Sirisha

    You can check this SAP Note 624627 - Sunday first day of the week also a working day (Collective)
    Might be helpful.
    Regards,
    AL

  • Want to make Monday as the first day of the week in GregorianCalendar. how?

    hi
    I need to know what day is the first of the month is. for example the 1st of Nov 2004 is Moday and 1st of Dec 2004 is saturday.
    I am using the GregorianCalendar:
    1.  GregorianCalendar calendar = new GregorianCalendar(2004,11,1);    //set date to 1st Nov 2004
    2.  int firstDay = calendar.get(Calendar.DAY_OF_WEEK);now firstDay is 2. This is because the week starts from Sunday, so Monday is the 2nd day.
    But I am in Uk and my Uk Calendar shows monday as the first day (even in Windows 2000 Calendar).
    I would like the GregorianCalendar to have Monday as the first day of the week, so that the
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    returns 1 in the above case.
    I also tried adding
    calendar.setFirstDayOfWeek(Calendar.MONDAY); just between line 1 and line 2, but it did not help.
    This is because i don't want to manually subtract 1. If i manually subtract one, then the program might not work in other locale and timezones.
    Also if I subtract 1, then for 1 Feb 2004,
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    will return 1 as 1st Feb 2004 falls on Sunday. so if i subtract 1 it will be 0, so I have to do a extra checking for 0.
    Is there anyway to make the Calendar have Monday as the first day of the week??
    Tanveer

    hi
    I need to know what day is the first of the month is.Why does this matter? Since we know that 1 == Sunday and 2 == Monday... Why do you need Monday to be == 1? and couldn't you just subtract 1 if it's so important?
    for example the 1st of Nov 2004 is Moday and 1st of
    Dec 2004 is saturday.
    I am using the GregorianCalendar:
    1.  GregorianCalendar calendar = new
    GregorianCalendar(2004,11,1);    //set date to 1st Nov
    2004
    2.  int firstDay = calendar.get(Calendar.DAY_OF_WEEK);now firstDay is 2. This is because the week starts
    from Sunday, so Monday is the 2nd day.
    But I am in Uk and my Uk Calendar shows monday as the
    first day (even in Windows 2000 Calendar).
    I would like the GregorianCalendar to have Monday as
    the first day of the week, so that the
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    returns 1 in the above case.
    I also tried adding
    calendar.setFirstDayOfWeek(Calendar.MONDAY);[/cod
    ] just between line 1 and line 2, but it did not help.
    This is because i don't want to manually subtract 1.
    If i manually subtract one, then the program might not
    work in other locale and timezones.
    Also if I subtract 1, then for 1 Feb 2004,
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    will return 1 as 1st Feb 2004 falls on Sunday. so if i
    subtract 1 it will be 0, so I have to do a extra
    checking for 0.
    Is there anyway to make the Calendar have Monday as
    the first day of the week??
    Tanveer

Maybe you are looking for

  • Notification Email should be triggered when hit the Apply changes?

    Hi All, Am creating a request form similiar to a remedy form,when ever the user create a new request and submit the request.Notification email will be sent to the user and the admin. Here when the admin ppl rejects the request,a notification email tr

  • Blue box with question mark

    I previously posted a question regarding a blue box with a question mark in it being displayed instead of a picture. I have updated my Flash player and I have ensured that everything in the quicktime section of the system preferences was checked (exc

  • Row lock contention...

    Hello, I am working on Oracle 10.2.0.4 on AIX . In awr reports we found 'row lock contention' as top wait events. As my knowledge There are two types of locks.. 1. Deadlock - where oracle will automatically rollback the locking query and generate dea

  • Jdeveloper/XDK latest with Oracle 9i r2

    Hi, I have a customer whos is asking a question regarding the compatibility of latest versions of Jdev & XDK developer tools. The question is pated below. Can anyone help me out in answering this customer? Its a high priority job. Thanks in advance.

  • QUESTION MARK ON MACBOOK PRO

    HI I am currently having issues in clearing the question mark. I have tried rebooting using the net but i can not locate the download disc space. And i have tried using the command button but nothing is working