Adding Dates in J2ME/midp
There appears to be a bug in manipulating dates in midp. I am trying to get the next 15 friday dates
from the current date (11/6/2001) by running the code below:
private Vector getNextWeekEndingDates(int numberOfWeeks)
{
Vector dates = new Vector();
String temp = "";
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int numberOfDaysTillFriday = Calendar.FRIDAY - dayOfWeek;
// set the first date to the friday of this week.
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth + numberOfDaysTillFriday);
for (int i=0; i< numberOfWeeks; i++)
{
year = cal.get(Calendar.YEAR);
dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH) + 1; // months are 0-11, adding 1 to get 1-12.
temp = month + "/" + dayOfMonth + "/" + year;
dates.addElement(temp);
// add 7 days to get the next week ending date.
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth + 7);
}
return dates;
}
in a J2SE environment I get what I expect:
11/9/2001
11/16/2001
11/23/2001
11/30/2001
12/7/2001
12/14/2001
12/21/2001
12/28/2001
1/4/2002
1/11/2002
1/18/2002
1/25/2002
2/1/2002
2/8/2002
2/15/2002
but in the J2ME/midp environment I get:
11/9/2001
11/16/2001
11/23/2001
11/30/2001
11/5/2001
11/12/2001
11/19/2001
11/26/2001
11/1/2001
11/8/2001
11/15/2001
11/22/2001
11/29/2001
11/4/2001
11/11/2001
Midp doesn't increment the month or the year. Is there a better
way to do this using midp or can this be fixed? Any help
greatly appreciated.

