在 Android 中以自定义格式显示相对日期

Display relative date with custom format in Android

我想将日期转换为人类可读的格式。我正在使用 DateUtils. getRelativeDateTimeString,但这不符合标准。我得到的输出看起来像:1 小时 15 分钟。以前等等

我想知道是否可以将格式更改为:

3 分钟 而不是 3 分钟。以前,

1 小时 而不是 1 小时。 15 分钟以前

使用 DateUtils 还是有其他方法可以做到这一点?

更准确地说,我正在寻找与此 angular-filter 等效的 Android,您可以在其中轻松更改相对日期的格式(例如:{{minutes}} minutes ago{{minutes}}m.

为了清楚起见,我不是在寻找格式化日期的方法,而是将日期转换为人类可读的格式,例如 'today'、“1 小时”、“38 分钟”(类似到 facebook 的相对日期)。

你为什么不把日期解析成你想要的格式?

使用 string.replace() 只需一行代码即可完成。

编辑 1:我通常这样使用 SimpleDateForma,希望对您有所帮助:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String currentDateandTime = sdf.format(new Date());

您将需要此导入(我认为 Android 会自动导入):

import java.text.SimpleDateFormat;
import java.util.Date;

编辑 2:在您的示例中,您需要做的是:

SimpleDateFormat sdf = new SimpleDateFormat("HH'h' mm'm'"); String currentDateandTime = sdf.format(new Date()); System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);

您应该使用 SimpleDateFormat 并指定您想要的模板。在你的情况下是这样的:

String template = "H'h', m'm'";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(template, Locale.getDefault());
String formatted = simpleDateFormat.format(new Date());

使用 built-in DateUtils 包含在 API 级别 3 中的实用程序库。

 CharSequence getRelativeDateTimeString (Context c, 
             long time, 
             long minResolution, 
             long transitionResolution, 
             int flags) Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]".

美国日期格式的示例输出字符串:

3 min. ago, 10:15 AM Yesterday, 12:20 PM Dec 12, 4:12 AM 11/14/2007, 8:20 AM

经过一些研究,我发现了一些像 Time4A, Joda-Time, PrettyTime, Android-Ago 这样的库。

但是,我决定不使用库并覆盖其文本资源,而是创建一个方法并将文本存储在 strings.xml 中以供将来可能的本地化使用。

    private static final int SECOND_MILLIS = 1000;
    private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
    private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
    private static final int WEEK_MILLIS = 7 * DAY_MILLIS;

    public static String getTimeAgo(Date date, Context context) {
        Date now = Calendar.getInstance().getTime();
        final long diff = now.getTime() - date.getTime();

        if (diff < SECOND_MILLIS) {
            return context.getString(R.string.just_now);
        } else if (diff < MINUTE_MILLIS) {
            return diff / SECOND_MILLIS + context.getString(R.string.seconds_ago);
        } else if (diff < 2 * MINUTE_MILLIS) {
            return context.getString(R.string.a_minute_ago);
        } else if (diff < 59 * MINUTE_MILLIS) {
            return diff / MINUTE_MILLIS + context.getString(R.string.minutes_ago);
        } else if (diff < 90 * MINUTE_MILLIS) {
            return context.getString(R.string.an_hour_ago);
        } else if (diff < 24 * HOUR_MILLIS) {
            return diff / HOUR_MILLIS + context.getString(R.string.hours_ago);
        } else if (diff < 48 * HOUR_MILLIS) {
            return context.getString(R.string.yesterday);
        } else if (diff < 6 * DAY_MILLIS) {
            return diff / DAY_MILLIS + context.getString(R.string.days_ago);
        } else if (diff < 11 * DAY_MILLIS) {
            return context.getString(R.string.a_week_ago);
        } else {
            return diff / WEEK_MILLIS + context.getString(R.string.weeks_ago);
        }
    }