/** * 将对比后的时间,格式化为:xx分钟前,xx小时前和日期 * * @param time 比对的时 * @return */ public static String convert_before(long time) { if (time < 0) return String.valueOf(time); int difftime = (int) ((System.currentTimeMillis() - time) / 1000); if (difftime < 86400 && difftime > 0) { if (difftime < 3600) { int min = (int) (difftime / 60); if (min == 0) return "刚刚"; else return (int) (difftime / 60) + "分钟"; } else { return (int) (difftime / 3600) + "小时"; } } else { Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(time); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE)) { return new SimpleDateFormat("HH:mm").format(c.getTime()); } if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) { return new SimpleDateFormat("昨天 HH:mm").format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 2) { return new SimpleDateFormat("前天 HH:mm").format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) { return new SimpleDateFormat("M月dd日 HH:mm").format(c.getTime()); } else { return new SimpleDateFormat("yy年M月dd日").format(c.getTime()); } } }
将对比后的时间,格式化为:xx分钟前,xx小时前和日期
最新推荐文章于 2022-03-22 18:49:18 发布