1

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?

2
  • Just a small hint: 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 of Calendar. Commented Feb 22, 2016 at 11:39
  • you can keep the same code and add the System.out.println() statement each time you add a day to your Calendar Commented Feb 22, 2016 at 11:48

5 Answers 5

1

You are assigning the reference of cStart to every of your variables. Use the clone() method, to get a new object every time.

Calendar cStart = Calendar.getInstance();

Calendar cMon = null;
Calendar cTue = null;
Calendar cWed = null;
Calendar cThu = null;
Calendar cFri = null;
Calendar cSat = null;
Calendar cSun = null;

cMon = (Calendar) cStart.clone();
cStart.add(Calendar.DAY_OF_MONTH, 1);
cTue = (Calendar) cStart.clone();
cStart.add(Calendar.DAY_OF_MONTH, 1);
cWed = (Calendar) cStart.clone();
cStart.add(Calendar.DAY_OF_MONTH, 1);
cThu = (Calendar) cStart.clone();
cStart.add(Calendar.DAY_OF_MONTH, 1);
cFri = (Calendar) cStart.clone();
cStart.add(Calendar.DAY_OF_MONTH, 1);
cSat = (Calendar) cStart.clone();
cStart.add(Calendar.DAY_OF_MONTH, 1);
cSun = (Calendar) cStart.clone();

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());

Output

Mon Feb 22 12:45:39 CET 2016
Tue Feb 23 12:45:39 CET 2016
Wed Feb 24 12:45:39 CET 2016
Thu Feb 25 12:45:39 CET 2016
Fri Feb 26 12:45:39 CET 2016
Sat Feb 27 12:45:39 CET 2016
Sun Feb 28 12:45:39 CET 2016
Sign up to request clarification or add additional context in comments.

Comments

1

Java 8 provides a new time API.
You could solve your problem quit nicely with it.

// There are various ways to set your start day
LocalDate start = LocalDate.now();

// Year - Month - Day
start = LocalDate.parse("2016-02-26");
start = LocalDate.of(2016,2,26);


LocalDate cMon = start.plusDays(1);
LocalDate cTue = start.plusDays(2);
LocalDate cWed = start.plusDays(3);
LocalDate cThu = start.plusDays(4);
LocalDate cFri = start.plusDays(5);
LocalDate cSat = start.plusDays(6);
LocalDate cSun = start.plusDays(7);

System.out.println(cMon);
System.out.println(cTue);
System.out.println(cWed);
System.out.println(cThu);
System.out.println(cFri);
System.out.println(cSat);
System.out.println(cSun);

Comments

0

Calendar cStart is a reference to a calendar object so when you do cMon = cStart; you are copying the reference to the same object. As you keep modifying the one and only Calendar object, all the references point to the same thing.

You can use clone() to get a copy of the object itself.

Comments

0
Calendar cStart = Calendar.getInstance();   
System.out.println(cStart.getTime());

     for(int i=0;i<6;i++){
       cStart.add(Calendar.DAY_OF_MONTH, 1);

       System.out.println(cStart.getTime());
     }

1 Comment

Please try to include some comments/explanation with your code to help others understand.
0

So you can use clone() as mentioned in the other answers to duplicate the Calendar instance for every weekday variable.
Or you use the Date class that is designed to store a Date while Calendar is designed to calculate Dates.

Calendar cStart = Calendar.getInstance();   

Date cMon = null;
Date cTue = null;
[...]

cMon = cStart.getTime();
cStart.add(Calendar.DAY_OF_MONTH, 1);
cTue = cStart.getTime();
cStart.add(Calendar.DAY_OF_MONTH, 1);
[...]

System.out.println(cMon);
System.out.println(cTue);
[...]

2 Comments

Class Calendar is not a singleton. Its (confusingly named) getInstance() method does not always return the same instance; it returns a new instance every time you call it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.