Results 1 to 8 of 8
  1. #1
    WimHaar is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Calendar mutator problem

    Hi,

    I have a problem using the set method in the Calendar class.

    When i try this:

    Java Code:
    public int changeDate (int newDate) {
            Calendar   cal = Calendar.getInstance();
            cal.set(Calendar.DATE, newDate);
            return  cal.get(Calendar.DATE); }
    everything is fine. The date gets set to whatever date it want to set it to.

    But now I implemented this in a class I made.

    Java Code:
    public class Calender {
        private Calendar cal;
    
     public Calender() {
         cal = Calendar.getInstance();
    And I made a test class, with this testmethod in it.

    Java Code:
        public void testGetDate() {
            System.out.println("getDate");
            Calender instance = new Calender();
            instance.getCal().set(Calendar.DATE, 20);
            int expResult = 20;
            int result = instance.getDate();
            assertEquals(expResult, result); }
    For some reason I can't get the date set to 20 in my testclass, it stays on 4.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by WimHaar View Post
    Java Code:
        public void testGetDate() {
            System.out.println("getDate");
            Calender instance = new Calender();
            instance.getCal().set(Calendar.DATE, 20);
            int expResult = 20;
            int result = instance.getDate();
            assertEquals(expResult, result); }
    For some reason I can't get the date set to 20 in my testclass, it stays on 4.
    You're not showing us the methods getCal() and getDate(); I think they're relevant here. Why aren't you using method changeDate( ... )?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    WimHaar is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You're not showing us the methods getCal() and getDate(); I think they're relevant here. Why aren't you using method changeDate( ... )?

    kind regards,

    Jos
    Java Code:
        public Calendar getCal () {
            return this.cal;
        }
    
        public int getDate () {
            return cal.get(Calendar.DATE);
        }
    and i found no changedate method in the api,
    only set() , add() and roll().

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by WimHaar View Post
    and i found no changedate method in the api,
    only set() , add() and roll().
    No, no, no, I meant your own changeDate( ... ) method. Can you show us your Calender class? It uses a Calendar instance and I think something is rotten there.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    WimHaar is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    No, no, no, I meant your own changeDate( ... ) method. Can you show us your Calender class? It uses a Calendar instance and I think something is rotten there.

    kind regards,

    Jos
    Oh ok, I guess I took kind of a silly name for my class.
    Calender and Calendar makes it a bit more complicated.

    I don't have a changeDate() method in my Calender class.


    If you want the whole class;

    Java Code:
    import java.util.*;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    
    
    
    public class Calender {
    
    
        private ArrayList<Appointment> appointments;
        private Calendar cal;
        private int date;
        private int month;
        private int year;
    
        /**
    	 * Constructor to create a new Calender object.
    	 * @param date the day of the month it is today.
             * @param year it is at the moment.
             * @param month the month of the year it is (jan = 1).
         */
    
     public Calender() {
         appointments = new ArrayList<Appointment>();
         cal = Calendar.getInstance();
         this.date = cal.get(Calendar.DATE);
         this.year = cal.get(Calendar.YEAR);
         this.month = cal.get(Calendar.MONTH)+1;
    
         
     }
    
    
             /**
    	 * Gets the appointments that take place during this day.
    	 * @return A list of the appointments that are planned during this day.
    	 */
    
        public ArrayList showAppointmentsDay (int date, int month, int year) {
            ArrayList<Appointment> appThisDay = new ArrayList<Appointment>();
            for(int i=0; i< appointments.size(); i++)
            {
                if(appointments.get(i).getDate() == date && appointments.get(i).getMonth() == month && appointments.get(i).getYear() == year)
                    appThisDay.add(appointments.get(i));
            }
            return appThisDay;
        }
    
             /**
    	 * Gets the appointments that take place during the rest of this week
    	 * @return A list of the appointments that are planned during the rest of this week.
    	 */
    
        
         public ArrayList showAppointmentsWeek() {
           ArrayList<Appointment> appThisWeek = new ArrayList<Appointment>();
           Calendar help;
           help = Calendar.getInstance();
           int n = 7 - getDayOfWeek() ;
           ArrayList<Appointment> appDay = new ArrayList<Appointment>();
           for(int i=0; i<=n; i++) {
               help.add(Calendar.DATE, i );
               appDay = null;
               appDay = showAppointmentsDay(help.get(Calendar.DATE), help.get(Calendar.MONTH), help.get(Calendar.YEAR));  //for every day of the week look for appointments
               for (int j=0; j<appDay.size(); j++)
               {
                   appThisWeek.add(appDay.get(j));
               }
           }
    
           return appThisWeek;
        }
        
    
    
            /**
    	 * Gets the date (day of the month it is).
    	 * @return The date.
    	 */
    
        public int getDate () {
            return this.date;
        }
            /**
    	 * Gets the year it is.
    	 * @return The year it is.
    	 */
    
        public int getYear () {
            return this.year;
        }
    
          /**
    	 * Gets the day of the week it is. (monday =1)
    	 * @return The day of the week it is.
    	 */
    
    
        public int getDayOfWeek() {
            return cal.get(Calendar.DAY_OF_WEEK)-1;
        }
    
              /**
    	 * Gets the current Calendar.
    	 * @return The  current Calendar.
    	 */
        public Calendar getCal () {
            return this.cal;
        }
            /**
    	 * Gets the month it is.
    	 * @return The month it is.
    	 */
    
        public int getMonth () {
            return this.month;
        }
     /**
    	 * Adds a new Appointment
    	 * @param The Appointment that needs to be added.
    	 */
        public void addAppointment(Appointment NewApp) {
            appointments.add(NewApp);
        }
    
       public void addDay() {
           this.cal.add(Calendar.DATE, 1);
       }
    
    }
    Everywhere I use the set() method there's a prolem.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    I found a solution to your original problem: your Calender doesn't know when its instance, a Calendar, is changed. it keeps a 'date' field that is never changed, no matter if you change the Calendar member. A first solution is to not keep those fields in your Calender class but ask the Calendar member object for those values, always.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    WimHaar is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I found a solution to your original problem: your Calender doesn't know when its instance, a Calendar, is changed. it keeps a 'date' field that is never changed, no matter if you change the Calendar member. A first solution is to not keep those fields in your Calender class but ask the Calendar member object for those values, always.

    kind regards,

    Jos
    Ok, thx Josh.
    Unbelievable that i didn't see this myself.

    Bedankt voor de hulp. :)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by WimHaar View Post
    Ok, thx Josh.
    Unbelievable that i didn't see this myself.

    Bedankt voor de hulp. :)
    Geen dank, graag gedaan. Sometimes I'm surprised by my own stupidity and blindness when I don't see an obvious bug in my code. Another pair of eyes can come in handy sometimes ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:21 AM
  2. Calendar date problem
    By groobelar in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 03:47 AM
  3. Calendar in java - problem
    By omygoodness in forum New To Java
    Replies: 7
    Last Post: 01-04-2010, 08:33 PM
  4. mutator method
    By dirtycash in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 10:29 PM
  5. Problem with calendar
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 08:39 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
  •