3

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

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

3m而不是3 分钟。以前

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

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

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

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

4

4 回答 4

2

经过一番研究,我发现了一些库,如Time4AJoda-TimePrettyTimeAndroid-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);
        }
    }
于 2017-02-16T11:16:53.533 回答
-1

使用 API 级别 3 中包含的内置DateUtils实用程序库。

 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 分钟。前 10:15 AM 昨天 12:20 PM 12 月 12 日 4:12 AM 2007 年 11 月 14 日 8:20 AM

于 2017-02-16T01:17:59.387 回答
-2

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

String template = "H'h', m'm'";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(template, Locale.getDefault());
String formatted = simpleDateFormat.format(new Date());
于 2017-02-15T16:00:12.427 回答
-2

为什么不将日期解析为您想要的格式?

使用 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);

于 2017-02-15T15:50:11.323 回答