2013-01-16 379 views
3

我想多少日期,小時,分鐘和秒其餘形式的日期1獲取多少時間(天,小時,分鐘)保持於分配日期時間(安卓)

String RemainingOn = "2013/25/1 22:36:24"; 
String mDate,mHour,mMinute,mSecond; 

public String DateComparedateTime() 
{ 
// compare RemainingOn with now 
    mDate = Difference date with RemainingOn and Now; 
    mHour = Difference hour with RemainingOn and Now; 
    mMinute = Difference minute with RemainingOn and Now; 
    mSecond = Difference second with RemainingOn and Now; 

    return mDate+","+mHour+","+mMinute+","+mSecond ; 
} 
+1

是否有任何理由不能傳遞'Date'或至少'long'而不是'String'? – seaplain

+0

是的,我想把返回結果放在textview – Saeed

+0

不是返回,而是傳入的參數 – seaplain

回答

2

下面的代碼提供想法

public String getDateDiffString(Date dateOne, Date dateTwo) 
{ 
    long timeOne = dateOne.getTime(); 
    long timeTwo = dateTwo.getTime(); 
    long oneDay = 1000 * 60 * 60 * 24; 
    long delta = (timeTwo - timeOne)/oneDay; 

    if (delta > 0) { 
     return "dateTwo is " + delta + " days after dateOne"; 
    } 
    else { 
     delta *= -1; 
     return "dateTwo is " + delta + " days before dateOne"; 
    } 
} 

如果你可以使用Joda Time

約達時間有時間Interval一個概念:

Interval interval = new Interval(oldTime, new Instant()); 
相關問題