4

I have implemented a method to get the date of the Monday in the current week, and I have assigned Monday to be the first day of the week.

But, no matter what I do, it returns Sun Mar 24 15:03:07 GMT 2013. I can't see what the issue is here. Anybody able to help?

public static Date getFirstDayOfWeekDate()
{
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(2);
    cal.set(Calendar.DAY_OF_WEEK,
    cal.getActualMinimum(Calendar.DAY_OF_WEEK));
    Date firstDayOfTheWeek = cal.getTime();
    return firstDayOfTheWeek;
}
Peter Bratton
  • 6,302
  • 6
  • 39
  • 61
user1899174
  • 279
  • 1
  • 5
  • 12

4 Answers4

3

try this :

public static Date getFirstDayOfWeekDate() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_WEEK,
            cal.getActualMinimum(Calendar.DAY_OF_WEEK));
    Date now = new Date();
    cal.setTime(now);
    int week = cal.get(Calendar.DAY_OF_WEEK);
    return new Date(now.getTime() - 24 * 60 * 60 * 1000 * (week - 1));
}
BlackJoker
  • 3,099
  • 2
  • 20
  • 27
1

It works for me:

    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(new Date());
    int today = c.get(Calendar.DAY_OF_WEEK);
    c.add(Calendar.DAY_OF_WEEK, -today+Calendar.MONDAY);
    System.out.println("Date "+c.getTime());
dreambit.io dreambitio
  • 1,892
  • 1
  • 15
  • 24
1

Just add one to the day of the week:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
return cal.getTime();
rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44
0

Or with JodaTime

LocalDate.now().withDayOfWeek(DateTimeConstants.MONDAY);

From Joda Time: First day of week?

Community
  • 1
  • 1
NilsH
  • 13,705
  • 4
  • 41
  • 59