把后台返回的一大串数字转换为对应时间

public static String getFormattedTimeString(long time) {
    Date date = new Date(time);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String result = format.format(date);
    long delta = new Date().getTime() - date.getTime();
    if (delta < 60000L) {
        long seconds = delta / 1000L;
        return (seconds <= 0 ? 1 : seconds) + "秒前";
    }
    if (delta < 3600000L) {
        long minutes = delta / 60000L;
        return (minutes <= 0 ? 1 : minutes) + "分钟前";
    }
    if (delta < 24L * 3600000L) {
        long hours = delta / 3600000L;
        return (hours <= 0 ? 1 : hours) + "小时前";
    }
    return result;
}

猜你喜欢

转载自blog.csdn.net/ZhangXuxiaoqingnian/article/details/82386320