Android 通话记录显示不同的时间格式

通话记录时间显示(昨天、今天等的时间格式)

1.今天:只显示时分
2.昨天:显示“昨天”
3.今年内,但是昨天以前,显示月/日
4.今年以前,显示年月日

//get date start总方法
    private String getFormatedDate(long date) {
        Resources r = mContext.getResources();
        //yesterday
        if (isYesterday(date)){
            return r.getString(R.string.call_log_date_yesterday);
        }
 
        //today
        if (isToday(date)){
            Time time = new Time();
            time.set(date);
            return time.format("%H:%M");
        }
 
        //this year,but before yesterday判断是否是今年内,却是昨天以前的时间
        if(isBeforeYesterday(date)){
            Time time = new Time();
            time.set(date);
            return time.format("%m/%d");
        }
 
        //before this year
        return getDate(date);
    }
 
    private final long TIME_MILLIS_OF_ONE_DAY = 1000 * 60 * 60 * 24;
 
    private boolean isYesterday(long when) {
        final Time time = new Time();
        time.set(when);
        final int thenYear = time.year;
        final int thenMonth = time.month;
        final int thenMonthDay = time.monthDay;
 
        time.set(System.currentTimeMillis() - TIME_MILLIS_OF_ONE_DAY);
        return (thenYear == time.year) && (thenMonth == time.month)
                && (thenMonthDay == (time.monthDay));
    }
 
    private boolean isToday(long data) {
        final Time time = new Time();
        time.set(data);
        final int thenYear = time.year;
        final int thenMonth = time.month;
        final int thenMonthDay = time.monthDay;
 
        time.set(System.currentTimeMillis());
        return (thenYear == time.year) && (thenMonth == time.month)
                && (thenMonthDay == (time.monthDay));
    }
 
    private boolean isBeforeYesterday(long when) {
        final Time time = new Time();
        time.set(when);
        final int thenYear = time.year;
 
        time.set(System.currentTimeMillis());
        return (thenYear == time.year);
    }
 
    private String getDate(long date) {
        Time time = new Time();
        time.set(date);
        return time.format("%Y/%m/%d");
    }
    //get date end
1+

留下评论