Android 以友好的方式显示时间

   /**
    * 以友好的方式显示时间
    *
    * @param sdate
    * @return
    */
   public static String friendly_time(String sdate) {
       Date time = null;


       if (TimeZoneUtil.isInEasternEightZones())
           time = toDate(sdate);
       else
           time = TimeZoneUtil.transformTime(toDate(sdate),
                   TimeZone.getTimeZone("GMT+08"), TimeZone.getDefault());


       if (time == null) {
           return "Unknown";
       }
       String ftime = "";
       Calendar cal = Calendar.getInstance();


       // 判断是否是同一天
       String curDate = dateFormater2.get().format(cal.getTime());
       String paramDate = dateFormater2.get().format(time);
       if (curDate.equals(paramDate)) {
           int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
           if (hour == 0)
               ftime = Math.max(
                       (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                       + "分钟前";
           else
               ftime = hour + "小时前";
           return ftime;
       }


       long lt = time.getTime() / 86400000;
       long ct = cal.getTimeInMillis() / 86400000;
       int days = (int) (ct - lt);
       if (days == 0) {
           int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
           if (hour == 0)
               ftime = Math.max(
                       (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                       + "分钟前";
           else
               ftime = hour + "小时前";
       } else if (days == 1) {
           ftime = "昨天";
       } else if (days == 2) {
           ftime = "前天 ";
       } else if (days > 2 && days < 31) {
           ftime = days + "天前";
       } else if (days >= 31 && days <= 2 * 31) {
           ftime = "一个月前";
       } else if (days > 2 * 31 && days <= 3 * 31) {
           ftime = "2个月前";
       } else if (days > 3 * 31 && days <= 4 * 31) {
           ftime = "3个月前";
       } else {
           ftime = dateFormater2.get().format(time);
       }
       return ftime;
   }

 

你可能感兴趣的:(Android)