# Rolling forward days in a calendar

• 08-04-2011, 03:13 PM
kumalh
Rolling forward days in a calendar
Hi everyone,

I'm writing my own calendar class and one of the methods I have to supply is the ability to roll the calendar date forward or backward by a given number of days. I'm not really sure how to go about doing this.

One way I was thinking was to write a method that adds one day to the date the calendar has, then put it in a loop and call that method for the amount of days needed. So if the given number of days is 7 for example, the method that adds one day will be called 7 times in a loop. However, when I tried to code this, I wasn't too sure. Also, I can't figure out what to do when the date reaches the end of the month.

For example if you were on the 25th of may and you added 10 days. It would be the 5th of the next month.

I'm obviously not asking for anyone to write the code for me, just point me in the right direction on how to go about doing this.

Cheers
• 08-04-2011, 03:16 PM
kumalh
Ahh sorry, I should mention that this is a set task, so I have to stick to the rules. I know there is already a calendar class, and I know that it has a add method, but I am required to write my own. Thanks!
• 08-04-2011, 03:58 PM
kumalh
Ok, this is what I have to add the number of days to the calendar -

Code:

```    public void addOne(boolean up) {         if(up == true)             date = date + 1;         else             date = date - 1;     }         public void rollDays(int d) {         if(d != 0)             if(d > 0)                 addOne(true);             else                 addOne(false);         }```
However, I still can't work out how to determine when it is the next month.
• 08-04-2011, 04:33 PM
Tolls
Presumably a map of some sort for month to days in a month?
• 08-04-2011, 04:35 PM
kumalh
The calendar currently has a date of the month, a month, and a year. Here is one of the constructors:

Code:

```    public Calendar(int d, int m, int y) {         date = d;         month = m;         year = y;     }```
• 08-04-2011, 04:57 PM
Tolls
So it knows nothing about calendars in general?
That is, the number of days in a month, or anything around leap years?

I would suggest putting that in place first, if only to validate the supplied values for a date in your constructor.
• 08-04-2011, 05:07 PM
kumalh
Ahh, that's what I was getting to next. I'm not exactly sure how to go about that.

For example, if somebody tries to create a calendar with month = 13, I want to prevent this, but I'm not sure how.
• 08-04-2011, 05:11 PM
kumalh

Perhaps I can create methods that validate if a given day or month value is within the correct range (days of months, months in year), then use those validate methods in the constructors themselves?
E.g:

Code:

```    public Calendar(int d, int m, int y) {         setDate(d);         setMonth(m);         setYear(y);     }```
With the 'setDate, setMonth, and setYear' methods being the validate methods?

Is that the correct line of thinking?
• 08-04-2011, 05:24 PM
Tolls
The problem with that is that you can't validate those values independently.
You need to validate the date as a whole.
After all d = 31 is valid for January but not for February.

The easiest for you is to have a static array of ints.
Those ints are the number days for that month...how you handle arrays being zero-based (ie array[0] is the first slot) is up to you.
Worry about leap years a bit further on.

Then check the maximum day (d) allowed for a month (m) by simply looking it up in the array.
• 08-04-2011, 05:37 PM
kumalh
I've already got the array of ints set up I've already got a method which finds if the month is a leap year and returns true if it is.

So to validate the given day for example, I've got this:
Code:

```    public void setDay(int d, int m) {         if( d <= DAYS_IN_MONTH[m] && d > 0)             date = d;             if(m == 2 && isLeapYear())                 date = 29;             else                 date = 28;     }```
To check whether the given day is within the range of that specific month.
Similarly for month, I have it validating that the month is in the range of 1 to 12.

My constructor now looks like this:
Code:

```    public void Calendar(int d, int m, int y) {         setDay(d, m);         setMonth(m);         setYear(y);     }```
• 08-04-2011, 05:51 PM
kumalh
So I just ran some tests on that and when I put in a day that is outside the range of the month (e.g. d = 42) It just sets the day to 0. How can I get it to throw up an error?

Also, ignore the isLeapYear part on the setDay method, I realised I got that wrong.
• 08-04-2011, 06:09 PM
Tolls
You'll need to throw an Exception.
That's the standard technique.

Check if that still falls within the max days for the month...if not then subtract those days for month from date and increment month.
Check again...and so on. When you get to month 12 and have to increment, then month becomes 1 and year increments.
Until you get to a month in which date is now valid.
• 08-04-2011, 06:16 PM
bnson
You try to function date.roll(int, int)

Code:

``` Calendar calendar = Calendar.getInstance(); calendar.roll(calendar.DAY_OF_YEAR, 4); //Set the next date. And calendar.roll(calendar.DAY_OF_YEAR, -4);  //Ste the previous date. // now get date calender Date tomorrow = calendar.getTime();```

Good luck...
• 08-04-2011, 06:40 PM
kumalh
I'm not sure I understand what you mean. Sorry if I'm being troublesome, I'm new to all of this :s:

Here is what I wrote from you just gave me:
Code:

```    public void rollDays(int d) {         int totalDays = DAYS_IN_MONTH[month];         int x = 0;         if(month == 2 && isLeapYear(year) == true)             totalDays = FEB_LEAP;                     date = date + d;         if(date > totalDays || d < 0)             x = date - totalDays;             month = month + 1;             date = x;```
And it's not working for me. It works when I go over the limit of the month (e.g. rolling 10 days forward from the 28th) but whenever I try to roll forward within the month (e.g. roll 2 days forward from the 5th) I just get d = 0, and m = the next month.
• 08-04-2011, 06:55 PM
Tolls
You're missing a whole load of brackets there.
Code:

```        if(date > totalDays || d < 0) {             x = date - totalDays;             month = month + 1;             date = x;         }```
That'll solve the problem so long as it doesn't have to move on more than a month.

Break out the days in month calculation into its own method and then call that to get the days in month:
Code:

`int totalDays = getDaysInCurrentMonth();`
We know that little bit works, and is useful elsewhere, so it should be its own method.
• 08-04-2011, 07:23 PM
kumalh
I have to be able to roll the calendar backwards also, so I changed to this method:

Code:

```        private void addOne(boolean up) {         if(up == true)             date = date + 1;         else             date = date - 1;     }         public void rollDaysTEST(int d) {         while(d != 0) {             if(d > 0){                 addOne(true);                         d = d - 1;  }             else {                 addOne(false);                         d = d + 1;  }            }     }```
However, I still have the same problems with recognising when it reaches the end of the month. I just can't seem to get it. Similarly, when rolling backwards, I have to decrease the month. Nothing seems to work for me.
• 08-04-2011, 08:03 PM
KevinWorkman
Crossposting makes Barack Obama have a sad birthday: Time an object is created