/**
* 获取显示时间
*
* @param time
* @return
*/
public String getDate(long time) {
if (time <= 0) {
return "";
}
// 日期处理
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int day = calendar.get(Calendar.DAY_OF_MONTH);
int year = calendar.get(Calendar.YEAR);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int miunte = calendar.get(Calendar.MINUTE);
int millisecond = calendar.get(Calendar.SECOND);
// 评论日期
Calendar commentCale = Calendar.getInstance();
commentCale.setTime(new Date(time));
int commDay = commentCale.get(Calendar.DAY_OF_MONTH);
int commYear = commentCale.get(Calendar.YEAR);
int commHour = commentCale.get(Calendar.HOUR_OF_DAY);
int commMiunte = commentCale.get(Calendar.MINUTE);
int commMonth = commentCale.get(Calendar.MONTH + 1);
int commMillisecond = commentCale.get(Calendar.SECOND);
// 刚刚
// X秒前
// X分钟前
// X小时前
// 昨天
// X月X日
// 2014年
if (year - commYear > 1) {// 去年 显示年
return commYear + "年";
}
if (year - commYear == 1) {// X月X日
return commMonth + "月" + commDay + "日";
}
if (day - commDay == 1) {// 昨天
return "昨天";
}
if (day - commDay == 0) {// 今天
if (hour == commHour) {// 同一小时
if (miunte == commMiunte) {// 同一分钟
if (millisecond == commMillisecond) {
return "刚刚";
}
return millisecond - commMillisecond + "秒前";
}
return miunte - commMiunte + "分钟前";
}
return hour - commHour + "小时前";
}
// 一般不会到这步
DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
return format.format(new Date(time));
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/liangrui1988/article/details/47100259