gpt4 book ai didi

android - 如何在 Android 上创建本地化的时间前字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:23 30 4
gpt4 key购买 nike

我正在查看 Google I/O Session 2012 app并遇到了这个 TODO

public static String getTimeAgo(long time, Context ctx) {
if (time < 1000000000000L) {
// if timestamp given in seconds, convert to millis
time *= 1000;
}

long now = getCurrentTime(ctx);
if (time > now || time <= 0) {
return null;
}

// TODO: localize
final long diff = now - time;
if (diff < MINUTE_MILLIS) {
return "just now";
} else if (diff < 2 * MINUTE_MILLIS) {
return "a minute ago";
} else if (diff < 50 * MINUTE_MILLIS) {
return diff / MINUTE_MILLIS + " minutes ago";
} else if (diff < 90 * MINUTE_MILLIS) {
return "an hour ago";
} else if (diff < 24 * HOUR_MILLIS) {
return diff / HOUR_MILLIS + " hours ago";
} else if (diff < 48 * HOUR_MILLIS) {
return "yesterday";
} else {
return diff / DAY_MILLIS + " days ago";
}
}

这让我想知道将其本地化的步骤是什么。

最佳答案

如果不需要自定义字符串,也可以使用DateUtils.getRelativeTimeSpanString ,它会产生如下字符串:

  • 42 分钟前
  • 10 秒前
  • 1 小时内

示例用法:

final CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(time, now, 0);

关于android - 如何在 Android 上创建本地化的时间前字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203018/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com