我使用以下代码来计算并返回两个日期之间的差值。
/**
* 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;
}
代码工作完美,除了偶尔,随机地,它吐出一个不正确的值。查看下面的日志,每秒更新一次,在上面代码的末尾创建(w/log .d):
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
正如您在上面看到的,三个日志行显示不一致的值。在我看来,time1和time2都很好,当获得两者之间的差异时,问题就出现了。
long diff = Math.abs(cal.getTimeInMillis() - t1);
你知道为什么会这样吗?
来自数学的javadocs。注意,如果参数等于Long的值。MIN_VALUE,最负的可表示长值,结果是相同的值,它是负的
如果(cal.getTimeInMillis() - t1)的结果为Long。MIN_VALUE你将在你的取模操作中使用负值。当然,这样做可能会导致意想不到的结果。