import java.util.Date; //导入方法依赖的package包/类
/**
* 转换时间
*
* @param now -1:直接格式化为yyyy-MM-dd HH:mm:ss
* 当前毫秒数:格式化为微博格式
* @param time
* @return
*/
public static String dateTransfer(long now, String time) {
long createAtTime = Date.parse(time);
Date createAt = new Date(createAtTime);
if (now != -1) {
long second = (now - createAtTime) / 1000L;
if (second < 0) {
second = 0;
}
if (second == 0) {
return "刚刚";
} else if (second < 30) {
return second + "秒以前";
} else if (second < 60) {
return "半分钟前";
} else if (second < 60 * 60) {
return (second / 60) + "分钟前";
} else if (second <= 60 * 60 * 3) {
return (second / 60 / 60) + "小时前";
} else if (getFormatTime(createAt, "d").equals(Calendar.getInstance().get(Calendar.DATE) + "")) {
return "今天 " + getFormatTime(createAt, "HH:mm");
} else if (getFormatTime(createAt, "d").equals((Calendar.getInstance().get(Calendar.DATE) - 1) + "")) {
return "昨天 " + getFormatTime(createAt, "HH:mm");
} else if (second < 60 * 60 * 24 * 7) {
return (((second / 60) / 60) / 24) + "天前";
} else if (getFormatTime(createAt, "yyyy").equals(Calendar.getInstance().get(Calendar.YEAR) + "")) {
// 同一年
return getFormatTime(createAt, "MM-dd HH:mm");
}
return getFormatTime(createAt, "yyyy-MM-dd HH:mm");
} else {
return getFormatTime(createAt, "yyyy-MM-dd HH:mm:ss");
}
}