Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does my date difference function spit out incorrect values randomly?
    primarykey
    data
    text
    <p>I'm using the following code to calculate and return the difference between two dates.. </p> <pre><code>/** * Calculate the absolute difference between two Date without * regard for time offsets * * @param d1 Date one * @param d2 Date two * @return The fields day, hour, minute, second and millisecond */ public static long[] getTimeDifference(Date d1, Date d2) { long[] result = new long[7]; Calendar cal = Calendar.getInstance(); cal.setTime(d1); long t1 = cal.getTimeInMillis(); cal.setTime(d2); long diff = Math.abs(cal.getTimeInMillis() - t1); long diffms = diff; final int ONE_SECOND = 1000; final int ONE_MINUTE = ONE_SECOND * 60; final int ONE_HOUR = ONE_MINUTE * 60; final int ONE_DAY = ONE_HOUR * 24; final int ONE_WEEK = ONE_DAY * 7; long w = diff / ONE_WEEK; diff %= ONE_WEEK; long d = diff / ONE_DAY; diff %= ONE_DAY; long h = diff / ONE_HOUR; diff %= ONE_HOUR; long m = diff / ONE_MINUTE; diff %= ONE_MINUTE; long s = diff / ONE_SECOND; long ms = diff % ONE_SECOND; result[0] = w; result[1] = d; result[2] = h; result[3] = m; result[4] = s; result[5] = ms; result[6] = diffms; Log.d("FTT", result[0] + "w, " + result[1] + "d, " + result[2] + "h, " + result[3] + "m, " + result[4] + "s, " + result[5] + "ms, " + result[6] + " diffms, " + "time1: " + t1 +", time2:" + cal.getTimeInMillis() ); return result; } </code></pre> <p>The code works perfectly, except every now and then, randomly, it spits out an incorrect value. See the following log, updated every second, created near the end of the above code (w/ Log.d):</p> <pre><code>07-28 17:20:29.225: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 30s, 772ms, 82470772 diffms, time1: 1311981300000, time2:1311898829228 07-28 17:20:30.226: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 29s, 773ms, 82469773 diffms, time1: 1311981300000, time2:1311898830227 07-28 17:20:31.233: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 28s, 772ms, 82468772 diffms, time1: 1311981300000, time2:1311898831228 07-28 17:20:32.226: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 27s, 773ms, 82467773 diffms, time1: 1311981300000, time2:1311898832227 07-28 17:20:33.226: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 26s, 772ms, 82466772 diffms, time1: 1311981300000, time2:1311898833228 07-28 17:20:34.227: DEBUG/FTT(2095): 7w, 1d, 15h, 57m, 13s, 67ms, 4377433067 diffms, time1: 1311981300000, time2:1311898834229 07-28 17:20:35.223: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 24s, 771ms, 82464771 diffms, time1: 1311981300000, time2:1311898835229 07-28 17:20:36.228: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 23s, 770ms, 82463770 diffms, time1: 1311981300000, time2:1311898836230 07-28 17:20:37.225: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 22s, 769ms, 82462769 diffms, time1: 1311981300000, time2:1311898837231 07-28 17:20:38.229: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 21s, 769ms, 82461769 diffms, time1: 1311981300000, time2:1311898838231 07-28 17:20:39.233: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 20s, 768ms, 82460768 diffms, time1: 1311981300000, time2:1311898839232 07-28 17:20:40.233: DEBUG/FTT(2095): 7w, 1d, 15h, 57m, 7s, 65ms, 4377427065 diffms, time1: 1311981300000, time2:1311898840231 07-28 17:20:41.233: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 18s, 768ms, 82458768 diffms, time1: 1311981300000, time2:1311898841232 07-28 17:20:42.233: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 17s, 768ms, 82457768 diffms, time1: 1311981300000, time2:1311898842232 07-28 17:20:43.233: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 16s, 766ms, 82456766 diffms, time1: 1311981300000, time2:1311898843234 07-28 17:20:44.233: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 15s, 766ms, 82455766 diffms, time1: 1311981300000, time2:1311898844234 07-28 17:20:45.233: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 14s, 765ms, 82454765 diffms, time1: 1311981300000, time2:1311898845235 07-28 17:20:46.234: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 13s, 765ms, 82453765 diffms, time1: 1311981300000, time2:1311898846235 07-28 17:20:47.234: DEBUG/FTT(2095): 7w, 1d, 15h, 57m, 0s, 60ms, 4377420060 diffms, time1: 1311981300000, time2:1311898847236 07-28 17:20:48.235: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 11s, 763ms, 82451763 diffms, time1: 1311981300000, time2:1311898848237 07-28 17:20:49.235: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 10s, 763ms, 82450763 diffms, time1: 1311981300000, time2:1311898849237 07-28 17:20:50.234: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 9s, 764ms, 82449764 diffms, time1: 1311981300000, time2:1311898850236 07-28 17:20:51.234: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 8s, 763ms, 82448763 diffms, time1: 1311981300000, time2:1311898851237 07-28 17:20:52.236: DEBUG/FTT(2095): 0w, 0d, 22h, 54m, 7s, 763ms, 82447763 diffms, time1: 1311981300000, time2:1311898852237 </code></pre> <p>As you can see above, three log lines show inconsistent values. It looks to me like time1 and time2 are fine, and the issue occurs when obtaining the difference between the two..</p> <pre><code>long diff = Math.abs(cal.getTimeInMillis() - t1); </code></pre> <p>Any ideas as to why this could be happening? I've wracked my brain and can't figure it out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload