https://blog.csdn.net/persistencegoing/article/details/84376427
第一种:
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
第二种:
/** * 把时间根据时、分、秒转换为时间段 * @param str 可选其1值{ "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" } * @author String ?日前或?小时前或?分钟前?秒前 */ public static String getTimes(Object str){ String resultTimes = ""; Date now; Date date=parseDate(str); now = new Date(); long times = now.getTime() - date.getTime(); long day = times / (24 * 60 * 60 * 1000); long hour = (times / (60 * 60 * 1000) - day * 24); long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60); long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); StringBuffer sb = new StringBuffer(); if(day>0){ sb.append(day + "天前"); }else if (hour > 0) { sb.append(hour + "小时前"); } else if (min > 0) { sb.append(min + "分钟前"); } else if (sec >0){ sb.append(sec + "秒前"); }else if(times>=0){ sb.append(times + "毫秒前"); }else{ sb.append("超前毫秒数:"+times); } resultTimes = sb.toString(); return resultTimes; }
希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职
群号:721 515 304