首页 > 其他 > 详细

时间戳转为代表"距现在多久之前"的字符串

时间:2014-10-30 15:37:03      阅读:171      评论:0      收藏:0      [点我收藏+]
/** 
 * 将时间戳转为代表"距现在多久之前"的字符串 
 * @param timeStr   时间戳 
 * @return 
 */  
public static String getStandardDate(String timeStr) {  
  
    StringBuffer sb = new StringBuffer();  
  
    long t = Long.parseLong(timeStr);  
    long time = System.currentTimeMillis() - (t*1000);  
    long mill = (long) Math.ceil(time /1000);//秒前  
  
    long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前  
  
    long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时  
  
    long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前  
  
    if (day - 1 > 0) {  
        sb.append(day + "天");  
    } else if (hour - 1 > 0) {  
        if (hour >= 24) {  
            sb.append("1天");  
        } else {  
            sb.append(hour + "小时");  
        }  
    } else if (minute - 1 > 0) {  
        if (minute == 60) {  
            sb.append("1小时");  
        } else {  
            sb.append(minute + "分钟");  
        }  
    } else if (mill - 1 > 0) {  
        if (mill == 60) {  
            sb.append("1分钟");  
        } else {  
            sb.append(mill + "秒");  
        }  
    } else {  
        sb.append("刚刚");  
    }  
    if (!sb.toString().equals("刚刚")) {  
        sb.append("前");  
    }  
    return sb.toString();  
}


时间戳转为代表"距现在多久之前"的字符串

原文:http://my.oschina.net/oppo4545/blog/338851

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!