2013-03-05 79 views
2

所以[DateUtils.getRelativeTimeSpanString()] [1]在Android SDK的偉大工程,爲顯示相對時間是在過去。DateUtils.getRelativeTimeSpanString未來日期

即:5天前,或5分鐘前。

但是,對於未來的日期來說,這似乎並不奏效。它似乎只是打印日期。

是否有產生相對時間跨度弦的日期是在未來(短寫的東西,通過比較兩個日曆對象計算出的天,時,分,秒),任何簡單的方法嗎?

東西長的線路:在5天,或在5分鐘內?

這裏基本上是我不得不做,它只是看起來有點髒的(注:此代碼只是爲了帖子撰寫並沒有真正通過Java編譯器運行):

Calendar calendarIO = Calendar.getInstance(); 
calendarIO.set(2013, 2, 14, 7, 0); 

long milliseconds1 = calendarIO.getTimeInMillis(); 
long milliseconds2 = System.currentTimeMillis(); 
long diff = milliseconds1 - milliseconds2; 
long diffSeconds = diff/1000; 
long diffMinutes = diff/(60 * 1000); 
long diffHours = diff/(60 * 60 * 1000); 
long diffDays = diff/(24 * 60 * 60 * 1000); 

String relativeTime = ""; 
if (diffDays > 1) { 
    relativeTime = diffDays + " days"; 
} else if (diffDays > 0) { 
    relativeTime = diffDays + " days " + diffHours + " hours"; 
} else if (diffHours > 1) { 
    relativeTime = diffHours + " hours"; 
} else if (diffMinutes > 0) { 
    relativeTime = diffMinutes + " minutes."; 
} 

回答

0
DateUtils.getRelativeTimeSpanString(endDate.getTime(), startDate.getTime(), DateUtils.FORMAT_ABBREV_RELATIVE); 

具有以下警告:

給出的明天而不是1天。