参数:   sdate = 2013-07-16 16:14:47

/**
* 以友好的方式显示时间
* @param sdate
* @return
*/
public static String friendly_time(String sdate) {
Date time = toDate(sdate);
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 <= 10){
ftime = days+"天前";
}
else if(days > 10){
ftime = dateFormater2.get().format(time);
}
return ftime;
} /**
* 将字符串转位日期类型
* @param sdate
* @return
*/
public static Date toDate(String sdate) {
try {
return dateFormater.get().parse(sdate);
} catch (ParseException e) {
return null;
}
} private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}; private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};

---恢复内容结束---

最新文章

  1. WCF wsHttpBinding之Transport security Mode, clientCredentialType=”Basic”
  2. Oracle 恢复被删除的数据,解决误操作删除数据
  3. Hibernate中的GetCurrentSession()方法
  4. iOS制作gif动态图
  5. Android ToggleButton使用介绍
  6. Linux,Unix各种版本的操作系统在线安装软件命令
  7. zzuli生化危机(dfs)
  8. Ubuntu18.04安装SS(不是服务器端!!!)
  9. Holer实现外网访问ARM嵌入式Linux系统
  10. CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-6CM安装前环境检查
  11. Python之Requests库的7个主要方法
  12. 获取当前最顶层的ViewController
  13. 【模板】Trie
  14. 【Alpha版本】冲刺阶段——Day3
  15. 无法创建.gitignore文件,提示必须输入文件名称
  16. Phoenix安装配置
  17. js 冷门的 label 语法
  18. C语言标准函数源代码
  19. SIT/UAT测试
  20. LeetCode OJ:Tenth Line(文件第十行)

热门文章

  1. day5模块学习--shelve模块
  2. Django实战(11):修改Model类
  3. 黑马程序员_java基础笔记(01)...java的环境搭建
  4. 微信公众号第三方平台生成自定义菜单提示 获取&quot;access_token失败&quot;
  5. jquery 无刷新添加/删除 input行 实时计算购物车价格
  6. ThinkPHP导入第三方类库Vendor
  7. Python网络编程之socket应用
  8. java UTF8 HEX
  9. 在chrome开发者工具中观察函数调用栈、作用域链、闭包
  10. ubuntu下 mysql安装以后无法登陆的的解决方法((ERROR 1698 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39;))