Trying to get the Monday date of the current week

I applied the method to get the Monday date of the current week, and I set Monday as the first day of the week.

But, no matter what I do, he returns Sun Mar 24 15:03:07 GMT 2013. I do not see that the problem is here. Can anybody 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; } 
+4
java date calendar
source share
4 answers

try the following:

 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)); } 
+3
source share

This 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()); 
+1
source share

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(); 
+1
source share

Or with JodaTime

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

From Time to Joda: First Day of the Week?

0
source share

All Articles