Results 1 to 17 of 17
  1. #1
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default 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

  2. #2
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    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!

  3. #3
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    Ok, this is what I have to add the number of days to the calendar -

    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);
        
        }
    However, I still can't work out how to determine when it is the next month.
    Last edited by kumalh; 08-04-2011 at 03:15 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    What data does your Calendar already have?
    Presumably a map of some sort for month to days in a month?

  5. #5
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    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.

  7. #7
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  8. #8
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    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:

    Java 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?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    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.

  10. #10
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    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:
    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;
        }
    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:
    Java Code:
        public void Calendar(int d, int m, int y) {
            setDay(d, m);
            setMonth(m);
            setYear(y);
        }

  11. #11
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    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.

  13. #13
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    You try to function date.roll(int, int)

    Java 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();
    Example: JavaDateRollExample

    Good luck...
    Last edited by bnson; 08-04-2011 at 05:19 PM.

  14. #14
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    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:
    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;
    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.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    You're missing a whole load of brackets there.
    Java 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:
    Java Code:
    int totalDays = getDaysInCurrentMonth();
    We know that little bit works, and is useful elsewhere, so it should be its own method.

  16. #16
    kumalh is offline Member
    Join Date
    Jul 2011
    Posts
    16
    Rep Power
    0

    Default

    I have to be able to roll the calendar backwards also, so I changed to this method:

    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;  }      
           }
        }
    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.

  17. #17
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    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

  1. Java coding for Rolling 2 Dices .. Can Any1 Help ??!!
    By Computer Science in forum New To Java
    Replies: 1
    Last Post: 11-10-2010, 04:03 PM
  2. Java rolling dice
    By fifa4ever in forum Java Applets
    Replies: 18
    Last Post: 06-10-2010, 08:43 PM
  3. Calendar days mixed up..
    By tomblue84 in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2010, 05:08 PM
  4. Java Program (rolling the dice)
    By genocist in forum New To Java
    Replies: 8
    Last Post: 03-01-2010, 03:15 PM
  5. Replies: 5
    Last Post: 07-29-2008, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •