关闭

[置顶] 模仿微博信息更新的时间:刚刚,10分钟前,半小时前,一天前

标签: android时间更新信息date显示时间的判断
606人阅读 评论(0) 收藏 举报
分类:

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










0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:102393次
    • 积分:1702
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:45篇
    • 译文:3篇
    • 评论:19条
    文章分类
    最新评论