Results 1 to 8 of 8
Thread: Calendar mutator problem
- 01-04-2011, 10:49 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Calendar mutator problem
Hi,
I have a problem using the set method in the Calendar class.
When i try this:
everything is fine. The date gets set to whatever date it want to set it to.Java Code:public int changeDate (int newDate) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DATE, newDate); return cal.get(Calendar.DATE); }
But now I implemented this in a class I made.
And I made a test class, with this testmethod in it.Java Code:public class Calender { private Calendar cal; public Calender() { cal = Calendar.getInstance();
For some reason I can't get the date set to 20 in my testclass, it stays on 4.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); }
- 01-04-2011, 11:09 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 01-04-2011, 11:31 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
- 01-04-2011, 11:37 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 01-04-2011, 11:49 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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;
Everywhere I use the set() method there's a prolem.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); } }
- 01-04-2011, 11:56 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-04-2011, 12:02 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
- 01-04-2011, 12:25 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Accessor/Mutator Question
By noble in forum New To JavaReplies: 4Last Post: 02-02-2010, 04:21 AM -
Calendar date problem
By groobelar in forum New To JavaReplies: 2Last Post: 01-17-2010, 03:47 AM -
Calendar in java - problem
By omygoodness in forum New To JavaReplies: 7Last Post: 01-04-2010, 08:33 PM -
mutator method
By dirtycash in forum New To JavaReplies: 7Last Post: 11-22-2007, 10:29 PM -
Problem with calendar
By Felissa in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 08:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks