为什么getRelativeTimeSpanString需要Context?

时间:2012-03-05 17:11:08

标签: java android

我想知道为什么getRelativeTimeSpanString需要上下文? (把双关语放在一边)

public static CharSequence getRelativeTimeSpanString (Context c, long millis, boolean withPreposition)

1 个答案:

答案 0 :(得分:2)

查看源代码:

public static CharSequence getRelativeTimeSpanString(Context c, long millis,
        boolean withPreposition) {

    long now = System.currentTimeMillis();
    long span = now - millis;

    if (sNowTime == null) {
        sNowTime = new Time();
        sThenTime = new Time();
    }

    sNowTime.set(now);
    sThenTime.set(millis);

    String result;
    int prepositionId;
    if (span < DAY_IN_MILLIS && sNowTime.weekDay == sThenTime.weekDay) {
        // Same day
        int flags = FORMAT_SHOW_TIME;
        result = formatDateRange(c, millis, millis, flags);
        prepositionId = R.string.preposition_for_time;
    } else if (sNowTime.year != sThenTime.year) {
        // Different years
        int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE;
        result = formatDateRange(c, millis, millis, flags);

        // This is a date (like "10/31/2008" so use the date preposition)
        prepositionId = R.string.preposition_for_date;
    } else {
        // Default
        int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
        result = formatDateRange(c, millis, millis, flags);
        prepositionId = R.string.preposition_for_date;
    }
    if (withPreposition) {
        Resources res = c.getResources();
        result = res.getString(prepositionId, result);
    }
    return result;
}