How to get all weekends for a given java date

I have a date and how do all dates fall in the week when this date belongs to java?

example: if i give today date then i should get all dates belonging to this week. 12 July 2015 to 18 July 2015 

Maybe someone can help me.

I am trying to get the week dates for a given date, I gave an explanation why this question is not duplicated. Please read before commenting.

+5
source share
3 answers

You can try the following:

 Calendar cal = Calendar.getInstance(); //cal.setTime(new Date());//Set specific Date if you want to for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) { cal.set(Calendar.DAY_OF_WEEK, i); System.out.println(cal.getTime());//Returns Date } 

OUTPUT

 Sun Jul 12 08:12:38 IST 2015 Mon Jul 13 08:12:38 IST 2015 Tue Jul 14 08:12:38 IST 2015 Wed Jul 15 08:12:38 IST 2015 Thu Jul 16 08:12:38 IST 2015 Fri Jul 17 08:12:38 IST 2015 Sat Jul 18 08:12:38 IST 2015 
+7
source

Pure Java 8 / java.time way

Based on this and this :

 public static List<LocalDate> datesOfWeekDate(LocalDate date) { LocalDate monday = date .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); return IntStream.range(0, 7).mapToObj(monday::plusDays).collect(toList()); } 
+2
source
  Calendar calendar = Calendar.getInstance(); int offset = calendar.get(Calendar.DAY_OF_WEEK) - 1; calendar.add(Calendar.DATE, -offset); for(int i = 0; i < 7; i++){ System.out.println(calendar.getTime()); calendar.add(Calendar.DATE, 1); } 
+1
source

All Articles