0

how to programically get the week first day date like if today is wednesday 11/9/2013 programically get the date of monday that is 9/9/2013 is there any method to get first day of week using current date? mDay give me today day number i want using this number to get day of start of week that is monday

   public static int mYear;
public static int mMonth;
public static int mDay;

    Calendar mCalendar = Calendar.getInstance();
    mYear = mCalendar.get(Calendar.YEAR);
    mMonth = mCalendar.get(Calendar.MONTH) + 1;
    mDay = mCalendar.get(Calendar.DAY_OF_MONTH);






        how to use this function below



          private static Date firstDayOfWeek(Date date) {
    Calendar calendar = Calendar.getInstance();
   calendar.setTime(date);
   calendar.set(Calendar.DAY_OF_WEEK, 1);
  return calendar.getTime();


wht parameter i write when call  function



                   firstDayOfWeek();?????


          }
user2762662
  • 99
  • 1
  • 1
  • 10

2 Answers2

0

Try this code it will solve your problem

public class dateTest {
    public static void main (String args  []) throws ParseException{
        DateFormat df=new SimpleDateFormat("dd/MM/yy");
        Date birthDay=df.parse("29/08/02");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MILLISECOND);
        cal.setTime(birthDay);

        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()+1);
        System.out.println("Date:       " + cal.get(Calendar.DATE)+ "/" +cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR));

    }
}

For getting the week day

Calendar cal=Calendar.getInstance();
       int dayNum=cal.get(Calendar.DAY_OF_WEEK);
       String day=new String();
       switch (dayNum){
           case 1:
            day="Sunday";
               break;
           case 2:
               day="Monday";
               break;
           case 3:
               day="Tuesday";
               break;
           case 4:
               day="Wednesday";
               break;
           case 5:
               day="Thursday";
               break;
           case 6:
               day="Friday";
               break;
           case 7:
               day="Saturday";
               break;

       }
       System.out.println("Day is: "+day);
Developer
  • 6,292
  • 19
  • 55
  • 115
  • it will return me date of monday of current week??? – user2762662 Sep 11 '13 at 08:16
  • if u will use cal.getTime() out put will be Sun Aug 25 00:00:00 IST 2002 or u can customize your output as i did cal.get(Calendar.DATE) similarly u can get every thing and concatenate it – Developer Sep 11 '13 at 08:22
  • i have current date like 9/11/2013 in input how do i use this input in any function to get current week monday date?? – user2762662 Sep 11 '13 at 08:25
  • see i have given u the complete idea now try it by yourself .one thing more i can do use Calendar.DAY_OF_WEEK this will give u 1,2,3,4 as create a hash map on it's key set 1 as monday 2 as tuesday so on .and u will get the output .i cann't spoon feed u still if u don't get it let me know but first try by yourself budddy – Developer Sep 11 '13 at 08:32
  • just tell me how do i filter ur output Sun Aug 25 00:00:00 IST 2002 to get only 25??? – user2762662 Sep 11 '13 at 08:37
  • what u want exactly let me know as input and output – Developer Sep 11 '13 at 09:32
0

You can simply use the code below: the add(int field, int amount) method of Calendar Class is suitable for your need.

int mYear = 0;
int mMonth;
int mDay;
int dayOFWeek = 0;
String mondayDate="";
Calendar mCalendar = Calendar.getInstance();
mCalendar.setFirstDayOfWeek(Calendar.MONDAY);
dayOFWeek = mCalendar.get(Calendar.DAY_OF_WEEK);        
mCalendar.add(Calendar.DATE, -dayOFWeek);

mYear = mCalendar.get(Calendar.YEAR);
mMonth = mCalendar.get(Calendar.MONTH) + 1;
mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
cahit beyaz
  • 4,829
  • 1
  • 30
  • 25