03-23-2009 07:41 AM
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
Solved! Go to Solution.
03-23-2009 07:49 AM
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.
03-23-2009 08:34 AM
Hi,
I've tried using the timezone but it didn't help either.
03-23-2009 08:41 AM - edited 03-23-2009 08:51 AM
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!
03-23-2009 08:44 AM
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 ).
03-23-2009 09:19 AM
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)
03-23-2009 09:45 AM
How many millis did it miss by and you didn't move the cast which is low precedence but
I always use parens here...
03-23-2009 10:07 AM
Hello,
Sorry, i guess i didn't understand your answer, can you please explain me which cast to remove from the code?
Thanks.
03-23-2009 10:16 AM
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...
03-23-2009 10:52 AM
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));