Getting start and end date of week using java calendar class

I want to get the last and first week of the week on a specific date. for example, if the date is October 12, 2011, I need the dates October 10, 2011 (as the start date of the week) and October 16, 2011 (as the end date of the week) Does anyone know how to get these 2 dates using the calendar class (java .util.Calendar) thanks a lot!

+8
java dayofweek calendar
source share
5 answers

Some code on how to do this with a Calendar object. I should also mention the joda time library as it can help you solve many Date/Calendar problems.

the code

 public static void main(String[] args) { // set the date Calendar cal = Calendar.getInstance(); cal.set(2011, 10 - 1, 12); // "calculate" the start date of the week Calendar first = (Calendar) cal.clone(); first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK)); // and add six days to the end date Calendar last = (Calendar) first.clone(); last.add(Calendar.DAY_OF_YEAR, 6); // print the result SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(df.format(first.getTime()) + " -> " + df.format(last.getTime())); } 
+32
source share

This solution works for any language (the first day of the week can be Sunday or Monday).

 Date date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek(); c.add(Calendar.DAY_OF_MONTH, -dayOfWeek); Date weekStart = c.getTime(); // we do not need the same day a week after, that why use 6, not 7 c.add(Calendar.DAY_OF_MONTH, 6); Date weekEnd = c.getTime(); 

For example, today is January 29, 2014. For a region with Sunday on the first day of the week, you will receive:

  start: 2014-01-26 end: 2014-02-01 

For a locale with Monday on the first day, the following dates will be indicated:

  start: 2014-01-27 end: 2014-02-02 
+2
source share

If you need all the dates, then

 first.add(Calendar.DAY_OF_WEEK,first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK)); for (int i = 1; i <= 7; i++) { System.out.println( i+" Day Of that Week is",""+first.getTime()); first.add(Calendar.DAY_OF_WEEK,1); } 
+1
source share

Here is a sample code

 public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(2016, 2, 15); { Calendar startCal = Calendar.getInstance(); startCal.setTimeInMillis(cal.getTimeInMillis()); int dayOfWeek = startCal.get(Calendar.DAY_OF_WEEK); startCal.set(Calendar.DAY_OF_MONTH, (startCal.get(Calendar.DAY_OF_MONTH) - dayOfWeek) + 1); System.out.println("end date : " + startCal.getTime()); } { Calendar endCal = Calendar.getInstance(); endCal.setTimeInMillis(cal.getTimeInMillis()); int dayOfWeek = endCal.get(Calendar.DAY_OF_WEEK); endCal.set(Calendar.DAY_OF_MONTH, endCal.get(Calendar.DAY_OF_MONTH) + (7 - dayOfWeek)); System.out.println("start date : " + endCal.getTime()); } } 

which will print

 start date : Sun Mar 13 20:30:30 IST 2016 end date : Sat Mar 19 20:30:30 IST 2016 
+1
source share

I found that the formula in the accepted answer will only work in some cases. For example, your week starts on Saturday, and today is Sunday. To arrive on the first day of the week, we return for 1 day, but the cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek() formula cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek() will give the answer -6. The solution is to use a module, so the formula is wrapped to speak.

 int daysToMoveToStartOfWeek = ( 7 + cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek() )%7; cal.add(Calendar.DAY_OF_WEEK, -1 * daysToMoveToStartOfWeek); 
0
source share

All Articles