Lucky_bo的专栏

不积跬步无以至千里,不积小流无以成江海

模仿微博信息更新的时间:刚刚,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 "";
}










阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lucky_bo/article/details/46820133
个人分类: Android开发
上一篇安卓实现序列化之Parcelable接口
下一篇ScrollView嵌套ListView冲突的解决
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭