7

i have a date and how to get all the dates fall on the week that the given date belongs in java?

example:

if i give today's date then i should get all dates belonging to this week.

12 July 2015 to 18 July 2015

could some one help me this please.

i am trying to get the week dates for the given date, I have given explanation why this question is not duplicate, Please read before commenting.

Joe
  • 4,460
  • 19
  • 60
  • 106
  • @TAsk the above question gives me dates for the current week, would you please help me to get the dates for the given date? since the above one takes the today's date and prints the current week but my question is to get the week dates for the given date – Joe Jul 17 '15 at 02:36
  • possible duplicate of [Get start and end of week on Android](http://stackoverflow.com/questions/6617854/get-start-and-end-of-week-on-android). Please search StackOverflow before posting. This topic has been addressed many times. – Basil Bourque Jul 17 '15 at 09:12

4 Answers4

13

You can try following way,

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
akash
  • 22,664
  • 11
  • 59
  • 87
4
    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);
    }
PythaLye
  • 314
  • 1
  • 8
4

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());
}
Community
  • 1
  • 1
Mateusz Szulc
  • 1,734
  • 19
  • 18
0

Return List with all days of the week

public static List<LocalDateTime> GetListOfTheWeekForDate(Date date){
  
  List<LocalDateTime> listOfWeek = new ArrayList<>(); 
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);

    for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
        
        cal.set(Calendar.DAY_OF_WEEK, i);
        TimeZone tz = cal.getTimeZone();
        ZoneId zoneId = tz.toZoneId();
        
        LocalDateTime localDateTime = LocalDateTime.ofInstant(cal.toInstant(), zoneId);
        listOfWeek.add(localDateTime);
        }
        
    return listOfWeek;
}
DavidT
  • 1
  • 1
    Thanks for wanting to contribute. This way is not recommended. You are mixing the old, poorly designed classes `Calendar` and `TimeZone`, with the modern and vastly better ones, `ZoneId` and `LocalDateTime`. Mixing always adds complication, and here it is completely needless. See in the answer by Mateusz Szulc how simply it can be done. – Ole V.V. Sep 24 '22 at 06:31
  • I tried your method and got `[2022-10-02T17:53:34.456, 2022-09-26T17:53:34.456, 2022-09-27T17:53:34.456, 2022-09-28T17:53:34.456, 2022-09-29T17:53:34.456, 2022-09-30T17:53:34.456, 2022-10-01T17:53:34.456]`. I don’t think that most users would want 17:53:34.456 each day? At least add some explanation in your answer why you chose this. Also, why are the days not in order? – Ole V.V. Sep 24 '22 at 06:35
  • You are using terrible date-time classes that were years ago supplanted by the modern *java.time* classes defined in JSR 310. Sun, Oracle, and the JCP community all gave up on these legacy classes. I suggest you do the same. See [*The Java Tutorials*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) by Oracle Corp. – Basil Bourque Sep 25 '22 at 00:24