方法一可以使用date的getTime()方法來將當前日期格式的時間轉換為毫秒數,進而相減。
long systime = new Date().getTime();//當前系統時間
long oldtime = old.getTime();//相比較的時間
Long time = (systime - oldtime);//相差毫秒數
方法二則使用calendar 的getTimeInMillis() 方法來將當前日期格式的時間轉換為毫秒數。
Calendar nowDate=Calendar.getInstance();
Calendar oldDate=Calendar.getInstance();
nowDate.setTime(new Date());//設置為當前系統時間
oldDate.setTime(old);//設置為想要比較的日期
Long timeNow=nowDate.getTimeInMillis();
Long timeOld=oldDate.getTimeInMillis();
Long time = (timeNow-timeOld);//相差毫秒數
得到兩個日期相差的毫秒數就可以得到兩個日期相差了幾天幾時幾分幾秒。