I’m having a small issue that I cannot seem to figure out. I am getting the time in milliseconds from two calanders, subtracting them, and dividing the result to get the difference of the two calanders in days. When done repeatedly sometimes this math will be off by a small decimal, usually .0000001. Is there a way to remedy this? Note: I need to keep these numbers as a long value because they will get huge after a while.
Heres the code:
First I get the calander date’s value in milliseconds…
final long calendarOne = datePickerCalendar
.getTimeInMillis();
final long calendarTwo = actualCalendar
.getTimeInMillis();
Then I subtract the two numbers…
if(calendarOne > calendarTwo)
{
long differenceInMilliseconds = (long)
(calendarOne) - (calendarTwo);
}
The value of differenceInMilliseconds will vary as follows with no changes between the two calendars.
Correct number: 63417600000
Incorrect number: 63417599999
Incorrect number: 63417599997
The math comes up with the correct number most of the time but I can’t figure out why there would be a discrepancy. Am I doing something wrong in my mathematics? Or is there a way to round longs?
Thanks for any help
After you create
actualCalendar, doactualCalendar.set( MILLISECOND, 0 ), also do the same ondatePickerCalendarand you will ensure that any difference between the two will be at least a second. Should fix your problem, since it’s probably due to delay between creating the two calendars.