方法
private static String timeCheckFun(long startTime, long endTime) {
long t = endTime - startTime;
int days = (int) (t / (1000 * 60 * 60 * 24));
int HH = (int) (t / (1000 * 60 * 60));
int mm = (int) (t / (1000 * 60));
int ss = (int) (t / (1000));
String beApart;
if (days <= 1) {
if (ss <= 60) {
beApart = "刚刚";
} else if (ss > 60 && ss <= 60 * 60) {
beApart = mm + "分钟前";
} else if (ss > 60 * 60 && ss <= 60 * 60 * 24) {
beApart = HH + "小时前";
} else {
beApart = days + "天前";
}
} else if (days < 30 && days > 1) {
beApart = days + "天前";
} else if (days >= 30 && days < 365) {
int days1 = days / 30;
beApart = days1 + "月前";
} else if (days >= 365) {
int days2 = days / 365;
beApart = days2 + "年前";
} else {
beApart = "历史很久远";
}
return beApart;
}
调用
timeCheckFun(((Date) obj[6]).getTime(), new Date().getTime())