You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java(Android)中将时间戳转为“一分钟前”式可读字符串

Android中Java实现时间戳转英文相对时间字符串

完全可行,以下两种方案都可以实现你需要的效果,直接输出String类型用于TextView展示:

方案一:使用Android系统自带工具类(简单高效)

Android提供了DateUtils类可以快速生成相对时间字符串,无需自己处理时间差计算,自动适配不同时间区间的文案:

import android.text.format.DateUtils;
import android.widget.TextView;

// 你的时间戳(毫秒级)
long timestamp = 1694603269045L;

// 生成相对时间字符串
CharSequence relativeTime = DateUtils.getRelativeTimeSpanString(
    timestamp,
    System.currentTimeMillis(),
    DateUtils.MINUTE_IN_MILLIS,
    DateUtils.FORMAT_ABBREV_RELATIVE | DateUtils.FORMAT_SHOW_DATE
);

// 转换为String后设置到TextView
String timeString = relativeTime.toString();
textView.setText(timeString);

该方法会自动根据时间差输出类似one minute ago、2 hours ago、3 days ago的格式,无需手动处理单复数和区间判断。

方案二:自定义工具类(高度定制化)

如果需要更精细控制文案内容(比如调整特殊场景的表述),可以自定义工具类实现:

public class TimeAgoUtils {
    private static final long SECOND_MILLIS = 1000;
    private static final long MINUTE_MILLIS = 60 * SECOND_MILLIS;
    private static final long HOUR_MILLIS = 60 * MINUTE_MILLIS;
    private static final long DAY_MILLIS = 24 * HOUR_MILLIS;
    private static final long WEEK_MILLIS = 7 * DAY_MILLIS;
    private static final long MONTH_MILLIS = 30 * DAY_MILLIS;
    private static final long YEAR_MILLIS = 365 * DAY_MILLIS;

    public static String getTimeAgo(long timestamp) {
        long now = System.currentTimeMillis();
        long diff = now - timestamp;
        
        // 处理未来时间的特殊情况
        if (diff < 0) {
            return "just now";
        }

        if (diff < MINUTE_MILLIS) {
            int seconds = (int) (diff / SECOND_MILLIS);
            return seconds == 1 ? "one second ago" : seconds + " seconds ago";
        } else if (diff < HOUR_MILLIS) {
            int minutes = (int) (diff / MINUTE_MILLIS);
            return minutes == 1 ? "one minute ago" : minutes + " minutes ago";
        } else if (diff < DAY_MILLIS) {
            int hours = (int) (diff / HOUR_MILLIS);
            return hours == 1 ? "one hour ago" : hours + " hours ago";
        } else if (diff < WEEK_MILLIS) {
            int days = (int) (diff / DAY_MILLIS);
            return days == 1 ? "one day ago" : days + " days ago";
        } else if (diff < MONTH_MILLIS) {
            int weeks = (int) (diff / WEEK_MILLIS);
            return weeks == 1 ? "one week ago" : weeks + " weeks ago";
        } else if (diff < YEAR_MILLIS) {
            int months = (int) (diff / MONTH_MILLIS);
            return months == 1 ? "one month ago" : months + " months ago";
        } else {
            int years = (int) (diff / YEAR_MILLIS);
            return years == 1 ? "one year ago" : years + " years ago";
        }
    }
}

使用方式:

long timestamp = 1694603269045L;
String timeAgo = TimeAgoUtils.getTimeAgo(timestamp);
textView.setText(timeAgo);

内容的提问来源于stack exchange,提问作者Newbie_programmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 01:52:18