2

是否有任何选项可以在一周中的某一天获取 getRelativeDateTimeString。表示如果日期在 7 天内显示为星期一,上午 7 点 14 分,而不是 4 天前,上午 7 点 14 分

DateUtils.getRelativeDateTimeString(context,
            datewithTime.getTime(), DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0)
4

1 回答 1

3

你好,你可以这样使用,

public class TimeUtils extends android.text.format.DateUtils {

    private static boolean isWithinWeek(final long millis) {
        return System.currentTimeMillis() - millis <= (WEEK_IN_MILLIS - DAY_IN_MILLIS);
    }

    private static boolean isWithinYear(final long millis) {
        return System.currentTimeMillis() - millis <= YEAR_IN_MILLIS;
    }

    public static String getRelativeTimeSpanString(final Context c, final long millis) {
        final String dateString;
        if (isToday(millis)) {
            dateString = formatDate(millis, "h:mm:ss a");
            // dateString = DateUtils.formatDateTime(c, millis, DateUtils.FORMAT_SHOW_TIME
            // | DateUtils.FORMAT_ABBREV_TIME);
        } else if (isWithinWeek(millis)) {
            dateString = DateUtils.formatDateTime(c, millis,
                    DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY);
        } else if (isWithinYear(millis)) {
            dateString = DateUtils.formatDateTime(c, millis,
                    DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR
                            | DateUtils.FORMAT_ABBREV_ALL);
        } else {
            dateString = DateUtils.formatDateTime(c, millis,
                    DateUtils.FORMAT_NUMERIC_DATE);
        }
        return dateString;
    }

    /**
     * To format the Current time
     * 
     * @param dateFormat
     * @return formated current time value.
     */
    public static String formatCurrentTime(String dateFormat) {
        return formatDate(System.currentTimeMillis(), dateFormat);
    }

    /**
     * To format the time in a readable given format.
     * 
     * @param milliSeconds - Time in milliseconds
     * @param dateFormat - Format date
     * @return formated time value.
     */
    public static String formatDate(long milliSeconds, String dateFormat) {
        if (dateFormat == null) {
            dateFormat = "hh:mm:ss.SSS";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

        /**
         * we are creating the calendar instance and setting the time value.
         */
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);

        return formatter.format(calendar.getTime());
    }
}
于 2017-06-13T08:56:23.400 回答