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

来源:互联网 发布:埃文蕾切尔伍德 知乎 编辑:程序博客网 时间:2024/03/29 10:19
  1. /** 
  2.  * 将时间戳转为代表"距现在多久之前"的字符串 
  3.  * @param timeStr   时间戳 
  4.  * @return 
  5.  */  
  6. public static String getStandardDate(String timeStr) {  
  7.   
  8.     StringBuffer sb = new StringBuffer();  
  9.   
  10.     long t = Long.parseLong(timeStr);  
  11.     long time = System.currentTimeMillis() - (t*1000);  
  12.     long mill = (long) Math.ceil(time /1000);//秒前  
  13.   
  14.     long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前  
  15.   
  16.     long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时  
  17.   
  18.     long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前  
  19.   
  20.     if (day - 1 > 0) {  
  21.         sb.append(day + "天");  
  22.     } else if (hour - 1 > 0) {  
  23.         if (hour >= 24) {  
  24.             sb.append("1天");  
  25.         } else {  
  26.             sb.append(hour + "小时");  
  27.         }  
  28.     } else if (minute - 1 > 0) {  
  29.         if (minute == 60) {  
  30.             sb.append("1小时");  
  31.         } else {  
  32.             sb.append(minute + "分钟");  
  33.         }  
  34.     } else if (mill - 1 > 0) {  
  35.         if (mill == 60) {  
  36.             sb.append("1分钟");  
  37.         } else {  
  38.             sb.append(mill + "秒");  
  39.         }  
  40.     } else {  
  41.         sb.append("刚刚");  
  42.     }  
  43.     if (!sb.toString().equals("刚刚")) {  
  44.         sb.append("前");  
  45.     }  
  46.     return sb.toString();  
  47. }  
1 1