Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
robybd
Posts: 216
Registered: ‎09-11-2008
My Device: 9000(Bold)
Accepted Solution

Comparing dates problem

Hello,

I'm trying to careate an app. for the BB with a trial period.

In order to impement the date comparison i'm using the class Calendar but it seems that this class

might have a bug:

i'm setting the start date to, for example, 28/04/2009 and i set the date on the BB to be 01/05/09

but when i'm comparing these dates i get 4 days difference instead of 3.

I noticed that it happens every time i'm comparing 2 dates on different months(e.g.: 24/03/2009 and

02/04/200), did anyone encountered this problem?

 

I'm using the following code:

Calendar nowCalender = Calendar.getInstance();
Calendar myCalender = Calendar.getInstance(); 

 

//read some info from the app. to get the required due date

//...

 

nowCalender.set(Calendar.YEAR, nowYear);//2009
nowCalender.set(Calendar.MONTH, nowMon);//05
nowCalender.set(Calendar.DAY_OF_MONTH, nowDay);//01
nowCalender.set(Calendar.HOUR_OF_DAY, 10);


myCalender.set(Calendar.YEAR, xmlYear);//2009
myCalender.set(Calendar.MONTH, xmlMon);//04
myCalender.set(Calendar.DAY_OF_MONTH, xmlDay);//28
myCalender.set(Calendar.HOUR_OF_DAY, 10);

 

long nowTime = nowCalender.getTime().getTime();
long myTime = myCalender.getTime().getTime();

 

float ret = (float)(nowTime - myTime) / 86400000; //get the no. of days between two dates

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Comparing dates problem

Quick "off the cuff" suggestion, not even looked at the code properly, so probably completely wrong, but is this a daylight saving issue, as we are heading towards that time of year?

 

Try:

Calendar nowCalender = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Calendar myCalender = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

 

Then the differences wont' include daylight saving.

Just a thought, probably wrong, if so apologies in advance.

Developer
robybd
Posts: 216
Registered: ‎09-11-2008
My Device: 9000(Bold)

Re: Comparing dates problem

Hi,

 

I've tried using the timezone but it didn't help either.

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Comparing dates problem

[ Edited ]

OK another quick suggestion,  I'm not sure about this code, because it requires lint/ong to float conversions and possible loss of significance:

 

float ret = (float)(nowTime - myTime) / 86400000; //get the no. of days between two dates

 

If all you want is the number of days, then I would use

 

long ret = (nowTime - myTime) / 86400000L; //get the no. of days between two dates

 

Note: I tend to use the DateTimeUtilities Constants rather than coding the number myself, for example

DateTimeUtilities.ONEDAY.

 

Edit: too quick - I forgot to spell check!

Message Edited by peter_strange on 03-23-2009 12:51 PM
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Comparing dates problem

I would put parens around your last cast and also check the raw long time difference-

how much does it miss by? I'm not sure if there are leap milliseconds at month

boundaries but it would help to isolate the issue if you eliminate "stochastic" things

like floating point ( LOL, integers operators are exact but on non-java the last few

digits on floats are usually random...  I'm simply pointing out here it can create some unpredicability ). 

Developer
robybd
Posts: 216
Registered: ‎09-11-2008
My Device: 9000(Bold)

Re: Comparing dates problem

Hi,

 

I've tried to use both suggestions but i still get the same results.

I've used:  long ret = (long)(nowTime - xmlTime) / DateTimeUtilities.ONEDAY;

to get the difference between the two dates and it's still returns wrong value (e.g.: 4 instead of 5)

 

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Comparing dates problem

How many millis did it miss by and you didn't move the cast which is low precedence but

I always use parens here...

 

Developer
robybd
Posts: 216
Registered: ‎09-11-2008
My Device: 9000(Bold)

Re: Comparing dates problem

Hello,

 

Sorry, i guess i didn't understand your answer, can you please explain me which cast to remove from the code?

Thanks.

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Comparing dates problem

any thing that confounds the result should be backed out- how many milliseconds do the

two times differ by? 

 

Then, do a % operation on the long and see what is left...

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Comparing dates problem

For example, after your line: 

long ret = (long)(nowTime - xmlTime) / DateTimeUtilities.ONEDAY;

 

you could add the following:

System.out.println("Now: " + Long.toString(nowTime) +

", XML: " + Long.toString(xmlTime) +

", Diff: " + Long.toString(nowTime - xmlTime) +

", Diff in Days: " + Long.toString(ret) +

", Remainder: " + Long.toString(nowTime - xmlTime) % DateTimeUtilities.ONEDAY));