Java - How to calculate the first and last day of each week

I am trying to create a weekly calendar that looks like this: http://dhtmlx.com/docs/products/dhtmlxScheduler/sample_basic.html

How can I calculate every week? For example, this week:

Monday Sunday
June 7, June 8, June 9, June 10, June 11, June 12, June 13.

+14
java calendar
Jun 11 '10 at 13:49 on
source share
11 answers

I think this does what you want:

// Get calendar set to current date and time Calendar c = Calendar.getInstance(); // Set the calendar to monday of the current week c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // Print dates of the current week starting on Monday DateFormat df = new SimpleDateFormat("EEE dd/MM/yyyy"); for (int i = 0; i < 7; i++) { System.out.println(df.format(c.getTime())); c.add(Calendar.DATE, 1); } 
+40
Jun 11
source share

The first day of this week.

  Calendar c = Calendar.getInstance(); while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) { c.add(Calendar.DATE, -1); } 
+8
Jun 11 '10 at 13:57 on
source share

Just setting the day of the week seems unreliable. Consider the following simple code:

 Calendar calendar = Calendar.getInstance(Locale.GERMANY); calendar.set(2011, Calendar.SEPTEMBER, 18); System.out.printf("Starting day: %tF%n", calendar); calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.printf("Last monday: %tF%n", calendar); System.out.printf("First day of week: %d%n", calendar.getFirstDayOfWeek()); 

The result of starting this program is:

 Starting day: 2011-09-18 Last monday: 2011-09-19 First day of week: 2 
In other words, he stepped forward in time. For German, this is really not the expected answer. Please note that the calendar correctly uses Monday as the first day of the week (perhaps only for calculating the week of the year).
+6
Sep 21 2018-11-11T00:
source share

With the new date and time API in Java 8, you should:

 LocalDate now = LocalDate.now(); // determine country (Locale) specific first day of current week DayOfWeek firstDayOfWeek = WeekFields.of(Locale.getDefault()).getFirstDayOfWeek(); LocalDate startOfCurrentWeek = now.with(TemporalAdjusters.previousOrSame(firstDayOfWeek)); // determine last day of current week DayOfWeek lastDayOfWeek = firstDayOfWeek.plus(6); // or minus(1) LocalDate endOfWeek = now.with(TemporalAdjusters.nextOrSame(lastDayOfWeek)); // Print the dates of the current week LocalDate printDate = startOfCurrentWeek; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE dd/MM/yyyy"); for (int i=0; i < 7; i++) { System.out.println(printDate.format(formatter)); printDate = printDate.plusDays(1); } 
+6
Jun 21 '15 at 16:44
source share

You can create the following: The following code prints the first and last dates of each week for 15 weeks.

 Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); for(int i=0; i<15; i++) { System.out.print("Start Date : " + c.getTime() + ", "); c.add(Calendar.DAY_OF_WEEK, 6); System.out.println("End Date : " + c.getTime()); c.add(Calendar.DAY_OF_WEEK, 1); } 
+2
Jun 11 2018-10-11
source share

Java.time

Using the java.time library built into Java 8:

 import java.time.DayOfWeek; import java.time.LocalDate; import static java.time.temporal.TemporalAdjusters.previousOrSame; import static java.time.temporal.TemporalAdjusters.nextOrSame; LocalDate now = LocalDate.now(); # 2015-11-23 LocalDate first = now.with(previousOrSame(DayOfWeek.MONDAY)); # 2015-11-23 LocalDate last = now.with(nextOrSame(DayOfWeek.SUNDAY)); # 2015-11-29 

You can DayOfWeek.values() over DayOfWeek.values() to get all current days of the week

 DayOfWeek.values(); # Array(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) for (DayOfWeek day: DayOfWeek.values()) { System.out.print(first.with(nextOrSame(day))); } # 2015-11-23, 2015-11-24, 2015-11-25, 2015-11-26, 2015-11-27, 2015-11-28, 2015-11-29 
+2
Nov 23 '15 at 10:23
source share

If you know which day (Friday) and current date (June 11), you can calculate other days this week.

+1
Jun 11 '10 at 13:55 on
source share

I recommend you use the Joda Library of Time. The Gregorian Calendar class has the weekOfWeekyear and dayOfWeek methods.

+1
Jun 11 '10 at 13:57 on
source share
  Calendar startCal = Calendar.getInstance(); startCal.setTimeInMillis(startDate); Calendar endCal = Calendar.getInstance(); endCal.setTimeInMillis(endDate); SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM-yyyy"); while (startCal.before(endCal)) { int weekNumber = startCal.get(Calendar.WEEK_OF_YEAR); Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); cal.set(Calendar.WEEK_OF_YEAR, weekNumber); Date sunday = cal.getTime(); Log.d("sunday", "" + sdf.format(sunday)); cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); cal.set(Calendar.WEEK_OF_YEAR, weekNumber); Date saturday = cal.getTime(); Log.d("saturday", "" + sdf.format(saturday)); weekNumber = weekNumber + 1; startCal.set(Calendar.WEEK_OF_YEAR, weekNumber); } 
+1
Apr 26 '16 at 4:21
source share
0
Jun 11 '10 at 13:54 on
source share

The algorithm you are looking for (calculating the day of the week for any given date) is Zeller Conduct. Here's the Java implementation:

http://technojeeves.com/joomla/index.php/free/57-zellers-congruence

0
Jun 11 '10 at 2:00
source share



All Articles