zoukankan      html  css  js  c++  java
  • Android GetTimeAgo(时间戳转换几天前,几分钟前,刚刚等)

    package com.studychen.seenews.util;
    
    import android.content.Context;
    
    /**
     * Created by tomchen on 2/26/16.
     */
    public class GetTimeAgo {
    
        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;
    
    
        /**
         * 按照毫秒来存储
         *
         * @param time
         * @return
         */
        public static String getTimeAgo(long time) {
            if (time < 1000000000000L) {
                // if timestamp given in seconds, convert to millis
                time *= 1000;
            }
    
            long now = System.currentTimeMillis();
            if (time > now || time <= 0) {
                return "未知时间";
            }
    
            final long diff = now - time;
    
            if (diff < MINUTE_MILLIS) {
                return "刚刚";
            } else if (diff < 2 * MINUTE_MILLIS) {
                return "1分钟前";
            } else if (diff < 50 * MINUTE_MILLIS) {
                return diff / MINUTE_MILLIS + "分钟前";
            } else if (diff < 90 * MINUTE_MILLIS) {
                return "1小时前";
            } else if (diff < 24 * HOUR_MILLIS) {
                return diff / HOUR_MILLIS + "小时前";
            } else if (diff < 48 * HOUR_MILLIS) {
                return "昨天";
            } else {
                return diff / DAY_MILLIS + "天前";
            }
        }
    }
  • 相关阅读:
    STL_算法_05_集合算法
    STL_算法_04_算术和生成算法
    STL_算法_03_拷贝和替换算法
    STL_算法_02_排序算法
    STL_算法_01_查找算法
    STL_容器使用时机
    STL_容器共通能力
    Qt5_选择文件对话框
    Qt5_当前exe所在路径
    Java 静态代理和动态代理
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/8252180.html
Copyright © 2011-2022 走看看