1

What I have: A today's Date-Object.

What I need: Storing the dates of the containing week in an ArrayList.

What have I tried:

public static void main(String[] args) {

    Date today = new Date();

    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setTime(today);

    List<Calendar> daysOfWeek = new ArrayList<>();

    IntStream
            .iterate(2, n -> n+1)
            .limit(7)
            .forEach(n ->
                {  
                    if(n != 8){
                        cal.set(Calendar.DAY_OF_WEEK, n);
                    }else{
                        cal.set(Calendar.DAY_OF_WEEK, 1);
                    }
                    System.out.println("CAL_INSIDE:  " + cal.getTime());
                    daysOfWeek.add(cal);
                });
             System.out.println("");
            daysOfWeek.stream().forEach( cal -> System.out.println("CAL_OUTSIDE: " + cal.getTime()));
}

Within the forEach-statement I get exactly what I want. But the ArrayList seems to be containing only the last Date. Output:

CAL_INSIDE:  Mon Aug 03 14:35:37 CEST 2015
CAL_INSIDE:  Tue Aug 04 14:35:37 CEST 2015
CAL_INSIDE:  Wed Aug 05 14:35:37 CEST 2015
CAL_INSIDE:  Thu Aug 06 14:35:37 CEST 2015
CAL_INSIDE:  Fri Aug 07 14:35:37 CEST 2015
CAL_INSIDE:  Sat Aug 08 14:35:37 CEST 2015
CAL_INSIDE:  Sun Aug 09 14:35:37 CEST 2015

CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:35:37 CEST 2015

Basically I get what I want but...

Question: Why is only sunday stored within the ArrayList ? What am I missing ?

EDIT

Output according to accepted answer:

CAL_INSIDE:  Mon Aug 03 14:51:31 CEST 2015
CAL_INSIDE:  Tue Aug 04 14:51:31 CEST 2015
CAL_INSIDE:  Wed Aug 05 14:51:31 CEST 2015
CAL_INSIDE:  Thu Aug 06 14:51:31 CEST 2015
CAL_INSIDE:  Fri Aug 07 14:51:31 CEST 2015
CAL_INSIDE:  Sat Aug 08 14:51:31 CEST 2015
CAL_INSIDE:  Sun Aug 09 14:51:31 CEST 2015

CAL_OUTSIDE: Mon Aug 03 14:51:31 CEST 2015
CAL_OUTSIDE: Tue Aug 04 14:51:31 CEST 2015
CAL_OUTSIDE: Wed Aug 05 14:51:31 CEST 2015
CAL_OUTSIDE: Thu Aug 06 14:51:31 CEST 2015
CAL_OUTSIDE: Fri Aug 07 14:51:31 CEST 2015
CAL_OUTSIDE: Sat Aug 08 14:51:31 CEST 2015
CAL_OUTSIDE: Sun Aug 09 14:51:31 CEST 2015
SklogW
  • 839
  • 9
  • 22
  • What about no duplicate because of the additional information that Calendar is not immutable ? – SklogW Aug 08 '15 at 12:54
  • If it was immutable, you would simply not be able to do it the way you did (you would have no way to change a field in an existing object), so this situation would not have arisen. So it certainly is the same problem as I mentioned. – RealSkeptic Aug 08 '15 at 12:56
  • you're right. thank you for the input – SklogW Aug 08 '15 at 12:57
  • As a rule of thumb, for `Calendar` never store it, return it or use it as parameter, use `Date` (or properly clone the `Calendar` if you have no other choice) –  Aug 08 '15 at 12:59
  • The stored calendar-objects are stored within a list in order to be passed as parameters, is this legit ? – SklogW Aug 08 '15 at 13:01

1 Answers1

3

Calendar is a mutable object so you just store references to that object and CAL_OUTSIDE only gives you 7 times the last inner state of the one referenced Calendar object.

You will have to move

Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(today);

inside your loop to get 7 different Calendar objects.

Sascha Kolberg
  • 7,092
  • 1
  • 31
  • 37