/** * 將對比後的時間,格式化爲: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小時前和日期
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.