java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = formatter.parse(“2015-10-15 17:10:58”);
holder.mtime.setText(StringUtils.checkNull(DateUtil.friendlyTime(date)));
}
catch (ParseException e) {
e.printStackTrace();
}
/**
* 以友好的方式显示时间
*
* @param time
* @return
*/
public static String friendlyTime(Date time) {
// 获取time距离当前的秒数
int ct = (int) ((System.currentTimeMillis() - time.getTime()) / 1000);
if (checkDateTime(time).equals("0")) {
if (ct <= 0) {
return "1分钟前";
}
if (ct > 0 && ct < 60) {
return "1分钟前";
}
if (ct >= 60 && ct < 3600) {
return Math.max(ct / 60, 1) + "分钟前";
}
if (ct >= 3600 && ct < 86400) {
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
String hour = df.format(time).substring(0, 2);
int hours = Integer.parseInt(hour);
if (hours > 12) {
return "下午" + df.format(time);
}
else {
return "上午" + df.format(time);
}
}
}
else if (checkDateTime(time).equals("1")) {
return "昨天";
}
else {
SimpleDateFormat mh = new SimpleDateFormat("yyyy-MM-dd");
String format = mh.format(time.getTime());
return format;
}
return ct / 31104000 + "年前";
}
原文:http://www.cnblogs.com/xidada/p/4934213.html