1.在评论中,根据添加时间转换为类似于 刚刚、几分钱、几天前、几个月前、几年前
直接上方法:
/**
* 将时间转换成显示的时间格式,参考微信
* @param addTime
* @return
*/
public static String getShowTime(Date addTime) {
//当前日历时间
Calendar currentCalendar = Calendar.getInstance();
//发布日历时间
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(addTime);
//当前时间单位
int cYear = currentCalendar.get(Calendar.YEAR);
int cMonth = currentCalendar.get(Calendar.MONTH);
int cDate = currentCalendar.get(Calendar.DAY_OF_MONTH);
int cHour = currentCalendar.get(Calendar.HOUR_OF_DAY);
int cMinuter = currentCalendar.get(Calendar.MINUTE);
//发布时间单位
int sYear = startCalendar.get(Calendar.YEAR);
int sMonth = startCalendar.get(Calendar.MONTH);
int sDate = startCalendar.get(Calendar.DAY_OF_MONTH);
int sHour = startCalendar.get(Calendar.HOUR_OF_DAY);
int sMinuter = startCalendar.get(Calendar.MINUTE);
if(cYear >sYear){
return (cYear - sYear) + "年前";
}
else if(cMonth > sMonth){
return (cMonth - sMonth) + "个月前";
}
else if(cDate > sDate){
return (cDate - sDate) > 1 ? (cDate - sDate) + "天前" : "昨天";
}
else if(cHour > sHour){
return (cHour - sHour) + "小时前";
}
else if(cMinuter >= sMinuter){
return (cMinuter - sMinuter) < 1 ? "刚刚" : (cMinuter - sMinuter) + "分钟前";
}
return "";
}
/* *根据客户端的时间信息得到发表评论的时间格式 *多少分钟前,多少小时前,然后是昨天,然后再是月日
Para : recordTime - {float} 时间戳 yearsFlag -{bool} 是否要年份 */
function getDiffTime(recordTime,yearsFlag) {
if (recordTime) {
recordTime=new Date(parseFloat(recordTime)*1000);
var minute = 1000 * 60,
hour = minute * 60,
day = hour * 24,
now=new Date(),
diff = now -recordTime;
var result = '';
if (diff < 0) {
return result;
}
var weekR = diff / (7 * day);
var dayC = diff / day;
var hourC = diff / hour;
var minC = diff / minute;
if (weekR >= 1) {
var formate='MM-dd hh:mm';
if(yearsFlag){
formate='yyyy-MM-dd hh:mm';
}
return recordTime.format(formate);
}
else if (dayC == 1 ||(hourC <24 && recordTime.getDate()!=now.getDate())) {
result = '昨天'+recordTime.format('hh:mm');
return result;
}
else if (dayC > 1) {
var formate='MM-dd hh:mm';
if(yearsFlag){
formate='yyyy-MM-dd hh:mm';
}
return recordTime.format(formate);
}
else if (hourC >= 1) {
result = parseInt(hourC) + '小时前';
return result;
}
else if (minC >= 1) {
result = parseInt(minC) + '分钟前';
return result;
} else {
result = '刚刚';
return result;
}
}
return '';
}