user2424507
user2424507

Reputation: 3

While statement with Calendar.after doesn't start

So I've spend a fair amount of Googling and looking through things, but I just can't get it to work. It won't consider the statement as true, even though the request.getEnd() is a couple days after the current startCal.getTime. Any ideas?

while(startCal.after(request.getEnd()))
            {
                /* Calculate new beginning */

                startCal.add(Calendar.DATE, 1);
                newBegin = startCal.getTime();
                System.out.println(newBegin);
                /* Calculate new ending */

                endCal.add(Calendar.DATE, 1);
                newEnd = endCal.getTime();

                /* Setting new dates and series ID */
                localRequest.setBegin(newBegin);
                localRequest.setEnd(newEnd);
                localRequest.setSeriesID(seriesID);

                /* Sending new reservation to database */
                //reserve(localRequest);
                System.out.println("RESERVATION DONE");
            }

Upvotes: 0

Views: 75

Answers (2)

Octahedron
Octahedron

Reputation: 901

Calendar#after returns whether this Calendar represents a time after the time represented by the specified Object.

So, your code is currently checking whether startCal is after request.getEnd(), which from your description is the opposite of what you're trying to do.

It would make more sense to use Calendar#before here to get the desired result.

Upvotes: 1

Affe
Affe

Reputation: 47994

even though the request.getEnd() is a couple days after the current startCal.getTime.

Then it makes sense that it wouldn't start! This line of code:

while(startCal.after(request.getEnd()))

Says "While start cal is after request end." You are describing your data as the opposite!

Upvotes: 4

Related Questions