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.

[1983 byte] By [lhbooker] at [2007-9-26 11:46:25]
# 1

[lhbooker],

Did you managed to resolve this issue? Can you post the entire MIDlet code so that I can try it on my machine here?

The Calendar method calls that you have in the code that you posted looks correct but as you have not posted the code that you used for the MIDlet version, it is quite difficult for me to predict where the problem lies.

This is an interesting phenomenon and I'ld like to help.

Allen Lai

Developer Technical Support

SUN Microsystems

http://www.sun.com/developers/support/

allenlai at 2007-7-2 1:57:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

The inconsistency between versions seems a little odd, however I think in eather case you chould be using cal.add() rather than cal.set().

Using cal.add() causes the calendar to do recalculations to its internal millisecond time representation immediatley whereas cal.set() just changes the chose field and flags it as having have changed.

Try cal.add() and see if that gives more expected results.

example:

cal.add(cal.DAY_OF_MONTH, 7);

lnoelstorr at 2007-7-2 1:57:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
Hi,I am so sorry but the method add does not exist in J2ME :(but i am very interested in this method:)
kgaluszka at 2007-7-2 1:57:49 > top of Java-index,Java Mobility Forums,Java ME Technologies...