Android: แสดงผล Datetime แบบให้คนอ่าน (Format relative date to human readable format in Android)

Android 6 ส.ค. 2015

สามารถใช้ Method จาก getRelativeTimeSpanString

public static String RelativeDate(Date date) {

        long timeInMilliseconds = date.getTime();
        long now = System.currentTimeMillis();
        CharSequence timeSpan = DateUtils.getRelativeTimeSpanString(timeInMilliseconds, now, DateUtils.HOUR_IN_MILLIS);

        return timeSpan.toString();
}

ที่นี้ก็จะสามารถแสดงผลได้แบบ “0 minutes ago” หรือ “Today” โดยจะขึ้นอยู่กับภาษาในเครื่องด้วย

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.