Results 1 to 17 of 17
- 08-04-2011, 02:13 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
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, 02:16 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
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, 02:58 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
Ok, this is what I have to add the number of days to the calendar -
However, I still can't work out how to determine when it is the next month.Java 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); }Last edited by kumalh; 08-04-2011 at 03:15 PM.
- 08-04-2011, 03:33 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
What data does your Calendar already have?
Presumably a map of some sort for month to days in a month?
- 08-04-2011, 03:35 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
The calendar currently has a date of the month, a month, and a year. Here is one of the constructors:
Java Code:public Calendar(int d, int m, int y) { date = d; month = m; year = y; }
- 08-04-2011, 03:57 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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, 04:07 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
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, 04:11 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
Just had a thought,
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:
With the 'setDate, setMonth, and setYear' methods being the validate methods?Java Code:public Calendar(int d, int m, int y) { setDate(d); setMonth(m); setYear(y); }
Is that the correct line of thinking?
- 08-04-2011, 04:24 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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, 04:37 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
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:
To check whether the given day is within the range of that specific month.Java 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; }
Similarly for month, I have it validating that the month is in the range of 1 to 12.
My constructor now looks like this:
Java Code:public void Calendar(int d, int m, int y) { setDay(d, m); setMonth(m); setYear(y); }
- 08-04-2011, 04:51 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
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.Last edited by kumalh; 08-04-2011 at 04:55 PM.
- 08-04-2011, 05:09 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You'll need to throw an Exception.
That's the standard technique.
Anyway, to go to your original problem I would take the input to addDays and add that value to your date field.
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, 05:16 PM #13
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
You try to function date.roll(int, int)
Example: JavaDateRollExampleJava 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...Last edited by bnson; 08-04-2011 at 05:19 PM.
- 08-04-2011, 05:40 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
I'm not sure I understand what you mean. Sorry if I'm being troublesome, I'm new to all of this

Here is what I wrote from you just gave me:
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.Java 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;
- 08-04-2011, 05:55 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You're missing a whole load of brackets there.
That'll solve the problem so long as it doesn't have to move on more than a month.Java Code:if(date > totalDays || d < 0) { x = date - totalDays; month = month + 1; date = x; }
Break out the days in month calculation into its own method and then call that to get the days in month:
We know that little bit works, and is useful elsewhere, so it should be its own method.Java Code:int totalDays = getDaysInCurrentMonth();
- 08-04-2011, 06:23 PM #16
Member
- Join Date
- Jul 2011
- Posts
- 16
- Rep Power
- 0
I have to be able to roll the calendar backwards also, so I changed to this method:
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.Java 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; } } }
- 08-04-2011, 07:03 PM #17
Crossposting makes Barack Obama have a sad birthday: Time an object is created
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Java coding for Rolling 2 Dices .. Can Any1 Help ??!!
By Computer Science in forum New To JavaReplies: 1Last Post: 11-10-2010, 04:03 PM -
Java rolling dice
By fifa4ever in forum Java AppletsReplies: 18Last Post: 06-10-2010, 08:43 PM -
Calendar days mixed up..
By tomblue84 in forum AWT / SwingReplies: 0Last Post: 03-17-2010, 05:08 PM -
Java Program (rolling the dice)
By genocist in forum New To JavaReplies: 8Last Post: 03-01-2010, 03:15 PM -
<jsp:include> Vs <%@include> Vs <jsp:forward> Vs RequestDispatcher .forward/includeVs
By freddieMaize in forum Java ServletReplies: 5Last Post: 07-29-2008, 02:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks