android 将时间戳转为代表"距现在多久之前"的字符串
2个回答
展开全部
public static final void testDate() {
//比如现在时间:2016/5/30 11:45:5
Date now = new Date(1464579905000l);
//上次时间: 2016/5/28 11:44:15
Date old = new Date(1464407055000l);
System.out.println(String.format("距现在%s之前", testPassedTime(now.getTime(), old.getTime())));
}
private static final long MINUTE_SECONDS = 60; //1分钟多少秒
private static final long HOUR_SECONDS = MINUTE_SECONDS*60;
private static final long DAY_SECONDS = HOUR_SECONDS*24;
private static final long YEAR_SECONDS = DAY_SECONDS*365;
public static String testPassedTime(long nowMilliseconds, long oldMilliseconds) {
long passed = (nowMilliseconds-oldMilliseconds) /1000;//转为秒
if (passed > YEAR_SECONDS) {
return passed/YEAR_SECONDS+"年";
} else if (passed > DAY_SECONDS) {
return passed/DAY_SECONDS+"天";
} else if (passed > HOUR_SECONDS) {
return passed/HOUR_SECONDS+"小时";
} else if (passed > MINUTE_SECONDS) {
return passed/MINUTE_SECONDS+"分钟";
} else {
return passed+"秒";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询