Phil
Phil

Reputation: 97

Adding multiple days to a Calendar in Java

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?

Upvotes: 1

Views: 1118

Answers (5)

arc
arc

Reputation: 4691

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

Upvotes: 1

ArcticLord
ArcticLord

Reputation: 4039

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);
[...]

Upvotes: 0

mounaim
mounaim

Reputation: 1180

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

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533670

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.

Upvotes: 0

kai
kai

Reputation: 6887

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

Upvotes: 1

Related Questions