I'm writing a program in which I need to work with dates. I'm receiving an Input date, which is the starting Day of a Week (Monday). In this case it is Mon Jan 05 00:00:00 CET 2015. Then I need to define the dates for the other days of the week.
I tried to do it this way:
Calendar cStart = Calendar.getInstance();
Calendar cMon = Calendar.getInstance();
Calendar cTue = Calendar.getInstance();
Calendar cWed = Calendar.getInstance();
Calendar cThu = Calendar.getInstance();
Calendar cFri = Calendar.getInstance();
Calendar cSat = Calendar.getInstance();
Calendar cSun = Calendar.getInstance();
cMon = cStart;
cStart.add(Calendar.DAY_OF_MONTH, 1);
cTue = cStart;
cStart.add(Calendar.DAY_OF_MONTH, 1);
cWed = cStart;
cStart.add(Calendar.DAY_OF_MONTH, 1);
cThu = cStart;
cStart.add(Calendar.DAY_OF_MONTH, 1);
cFri = cStart;
cStart.add(Calendar.DAY_OF_MONTH, 1);
cSat = cStart;
cStart.add(Calendar.DAY_OF_MONTH, 1);
cSun = cStart;
System.out.println(cMon.getTime());
System.out.println(cTue.getTime());
System.out.println(cWed.getTime());
System.out.println(cThu.getTime());
System.out.println(cFri.getTime());
System.out.println(cSat.getTime());
System.out.println(cSun.getTime());
Now my problem is, that the output should look like this:
Mon Jan 05 00:00:00 CET 2015
Tue Jan 06 00:00:00 CET 2015
Wed Jan 07 00:00:00 CET 2015
Thu Jan 08 00:00:00 CET 2015
Fri Jan 09 00:00:00 CET 2015
Sat Jan 10 00:00:00 CET 2015
Sun Jan 11 00:00:00 CET 2015
But actually it is looking like this:
Sun Jan 11 00:00:00 CET 2015
Sun Jan 11 00:00:00 CET 2015
Sun Jan 11 00:00:00 CET 2015
Sun Jan 11 00:00:00 CET 2015
Sun Jan 11 00:00:00 CET 2015
Sun Jan 11 00:00:00 CET 2015
Sun Jan 11 00:00:00 CET 2015
What can I do, to receive the output that I need?
Calendar.getInstance()always returns the same physical object. So every variable (cMon,cTue, etc.) you're using is pointing to exactly the same object. Try using new instance ofCalendar.