3

I try to get the date of monday used the codes below (today is 2016/5/6)

Calendar calendar1 = Calendar.getInstance();
calendar1.setFirstDayOfWeek(Calendar.MONDAY);
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar1.getTime();

But the result was 2016/4/25, and the result should be the 2016/5/2, and it seems that just wrong in May

And it work correct below

Calendar calendar1 = Calendar.getInstance();
calendar1.setFirstDayOfWeek(Calendar.MONDAY);
calendar1.setTime(new Date());
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar1.getTime();

But while I try below

  public static Calendar createCalendar(Date date, int firstDayOfWeek) {
    Calendar calendar = Calendar.getInstance();
    if (date == null) return calendar;
    calendar.setFirstDayOfWeek(firstDayOfWeek);
    calendar.setTime(date);
    return calendar;
  }

  public static Calendar[] getWeekStartEnd(Date date) {
    Calendar[] calendars = new Calendar[2];
    Calendar calendar = createCalendar(date, Calendar.MONDAY);
    Calendar tempCalendar1 = createCalendar(date, Calendar.MONDAY);

    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    tempCalendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

    calendars[0] = calendar;
    calendars[1] = tempCalendar1;
    return calendars;
  }

It still not work correct And I want to know the reason

littlegnal
  • 425
  • 4
  • 14
  • Please see this link [link](http://stackoverflow.com/questions/7299621/android-calendar-problem-with-day-of-the-week) – Sabari May 09 '16 at 10:33

2 Answers2

0

May you need 1st day of current Week (Suppose current date ==> 2016/5/5 so you may required 1st day ==> 2016/5/2 which is Monday then)

Calendar calendar = Calendar.getInstance();
// get device day
SimpleDateFormat formatter = new SimpleDateFormat("EEE");
String devicedaystring = formatter.format(calendar.getTime());
//get Device date
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String devicedatestring = f.format(calendar.getTime());

String current_date = checkday(devicedatestring, devicedaystring);
// To Convert String date to Date object
SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = f1.parse( current_date);

private String checkDay(String date, String day)
    {
        if(day.equalsIgnoreCase("MON"))
        {
            return date;
        }
        else
        {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            String[] ss=date.split("-");
            int yy = Integer.parseInt(ss[0]);
            int MM = Integer.parseInt(ss[1]);
            int dd = Integer.parseInt(ss[2]);
            Calendar calendar = Calendar.getInstance();
            calendar.set(yy,MM-1,dd);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            date =  f.format(calendar.getTime());
            Logger.showVerboesLog("~~~", "First Day of Week ===  " +date);
            return date;
        }
    }
Ankit Gomkar
  • 27
  • 11
0

Try this:

 Calendar calendar1 = Calendar.getInstance();
                calendar1.setFirstDayOfWeek(Calendar.MONDAY);
                calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                calendar1.setTime(new Date());
                calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                calendar1.getTime();
Sabari
  • 1,963
  • 1
  • 13
  • 10
  • I found that it correct by `calendar1.setTime(new Date())` again, no matter `calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)` set twice, and I think I should update my question – littlegnal May 07 '16 at 10:28
  • Sorry, I don't know the reason. While debugging the code line by line it is working good. But while running the same code, it is returning previous week monday date. – Sabari May 07 '16 at 10:28