2013-10-16 70 views
13

我試圖創建一個自定義格式來顯示經過的時間。相對時間跨度的自定義格式

現在我使用的是:

CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(
    dateObject.getTime(), 
    System.currentTimeMillis(), 
    DateUtils.SECOND_IN_MILLIS, 
    flags); 

這將返回相對時間跨度是這樣的:

  • 1分鐘前
  • 37分鐘前
  • 2小時前

...

我想要做的是顯示這一點,就像這樣:

  • 1米
  • 36米
  • 4H

...

我知道DateUtils已FORMAT_ABBREV_ALL標誌,但這並不像我想要的那樣縮寫字符串...

如何爲此創建自定義內容?

回答

8

我已經在我的Twitter模塊

public static String getTimeString(Date fromdate) { 

    long then; 
    then = fromdate.getTime(); 
    Date date = new Date(then); 

    StringBuffer dateStr = new StringBuffer(); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(date); 
    Calendar now = Calendar.getInstance(); 

    int days = daysBetween(calendar.getTime(), now.getTime()); 
    int minutes = hoursBetween(calendar.getTime(), now.getTime()); 
    int hours = minutes/60; 
    if (days == 0) { 

     int second = minuteBetween(calendar.getTime(), now.getTime()); 
     if (minutes > 60) { 

      if (hours >= 1 && hours <= 24) { 
       dateStr.append(hours).append("h"); 
      } 

     } else { 

      if (second <= 10) { 
       dateStr.append("Now"); 
      } else if (second > 10 && second <= 30) { 
       dateStr.append("few seconds ago"); 
      } else if (second > 30 && second <= 60) { 
       dateStr.append(second).append("s"); 
      } else if (second >= 60 && minutes <= 60) { 
       dateStr.append(minutes).append("m"); 
      } 
     } 
    } else 

    if (hours > 24 && days <= 7) { 
     dateStr.append(days).append("d"); 
    } else { 
     dateStr.append(twtimeformat.format(date)); 
    } 

    return dateStr.toString(); 
} 

public static int minuteBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.SECOND_IN_MILLIS); 
} 

public static int hoursBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.MINUTE_IN_MILLIS); 
} 

public static int daysBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.DAY_IN_MILLIS); 
} 
+1

謝謝。最後,我實現了類似的東西。如果存在像我的問題那樣簡單的方法,但後面還有一些定製,我寧願選擇。 仍然是一個非常好的答案。我會將其標記爲解決方案。 –

3

創建這樣

public static String getDateDifferenceForDisplay(Date inputdate) { 
    Calendar now = Calendar.getInstance(); 
    Calendar then = Calendar.getInstance(); 

    now.setTime(new Date()); 
    then.setTime(inputdate); 

    // Get the represented date in milliseconds 
    long nowMs = now.getTimeInMillis(); 
    long thenMs = then.getTimeInMillis(); 

    // Calculate difference in milliseconds 
    long diff = nowMs - thenMs; 

    // Calculate difference in seconds 
    long diffMinutes = diff/(60 * 1000); 
    long diffHours = diff/(60 * 60 * 1000); 
    long diffDays = diff/(24 * 60 * 60 * 1000); 

    if (diffMinutes < 60) { 
     return diffMinutes + " m"; 

    } else if (diffHours < 24) { 
     return diffHours + " h"; 

    } else if (diffDays < 7) { 
     return diffDays + " d"; 

    } else { 

     SimpleDateFormat todate = new SimpleDateFormat("MMM dd", 
       Locale.ENGLISH); 

     return todate.format(inputdate); 
    } 
} 

獲取日期差異的一種方法,那麼你可以這樣調用

String dateForDisplay = getDateDifferenceForDisplay(inputDate); 
1

有這樣做沒有內置的方法/實用程序,但使用可用的0來實現它非常簡單常量:

private static final String ABBR_YEAR = "y"; 
private static final String ABBR_WEEK = "w"; 
private static final String ABBR_DAY = "d"; 
private static final String ABBR_HOUR = "h"; 
private static final String ABBR_MINUTE = "m"; 

public static String getAbbreviatedTimeSpan(long timeMillis) { 
    long span = Math.max(System.currentTimeMillis() - timeMillis, 0); 
    if (span >= DateUtils.YEAR_IN_MILLIS) { 
     return (span/DateUtils.YEAR_IN_MILLIS) + ABBR_YEAR; 
    } 
    if (span >= DateUtils.WEEK_IN_MILLIS) { 
     return (span/DateUtils.WEEK_IN_MILLIS) + ABBR_WEEK; 
    } 
    if (span >= DateUtils.DAY_IN_MILLIS) { 
     return (span/DateUtils.DAY_IN_MILLIS) + ABBR_DAY; 
    } 
    if (span >= DateUtils.HOUR_IN_MILLIS) { 
     return (span/DateUtils.HOUR_IN_MILLIS) + ABBR_HOUR; 
    } 
    return (span/DateUtils.MINUTE_IN_MILLIS) + ABBR_MINUTE; 
} 
+0

除了提前一天外,這很好!任何想法爲什麼是這樣? – Tina