模仿微博信息更新的时间:刚刚,10分钟前,半小时前,一天前
榜样微博信息更新的时间:刚刚,10分钟前,半小时前,一天前
1.首先设置每一个阶段的时间:
private static final int seconds_of_1minute = 60;
private static final int seconds_of_30minutes = 30 * 60;
private static final int seconds_of_1hour = 60 * 60;
private static final int seconds_of_1day = 24 * 60 * 60;
private static final int seconds_of_15days = seconds_of_1day * 15;
private static final int seconds_of_30days = seconds_of_1day * 30;
private static final int seconds_of_6months = seconds_of_30days * 6;
private static final int seconds_of_1year = seconds_of_30days * 12;
2.详细推断的方法:
/**
* @return timtPoint距离如今经过的时间,分为
* 刚刚。1-29分钟前,半小时前。1-23小时前,1-14天前,半个月前,1-5个月前,半年前,1-xxx年前
*/
public String getTimeElapse() {
long nowTime = new Date().getTime() / 1000;
//createTime是发表文章的时间
long oldTime = createTime;
//elapsedTime是发表和如今的间隔时间
long elapsedTime = nowTime - oldTime;
if (elapsedTime < seconds_of_1minute) {
return "刚刚";
}
if (elapsedTime < seconds_of_30minutes) {
return elapsedTime / seconds_of_1minute + "分钟前";
}
if (elapsedTime < seconds_of_1hour) {
return "半小时前";
}
if (elapsedTime < seconds_of_1day) {
return elapsedTime / seconds_of_1hour + "小时前";
}
if (elapsedTime < seconds_of_15days) {
return elapsedTime / seconds_of_1day + "天前";
}
if (elapsedTime < seconds_of_30days) {
return "半个月前";
}
if (elapsedTime < seconds_of_6months) {
return elapsedTime / seconds_of_30days + "月前";
}
if (elapsedTime < seconds_of_1year) {
return "半年前";
}
if (elapsedTime >= seconds_of_1year) {
return elapsedTime / seconds_of_1year + "年前";
}
return "";
}