微信时间显示规则:
-
今天: HH:mm ,例 8:28
-
昨天: 昨天 HH:mm, 例 昨天 9:27
-
近7天 : 星期X HH:mm ,例 星期一 6:25
-
今年: M月d日 HH:mm 例 3月2日 9:43
-
往年: yyyy年M月d日 HH:mm 例 2018年6月9日 6:52
public static String getTimeString(Long timestamp) { String result = ""; int[] weekNames = {星期日, 星期一, 星期日二, 星期日三, 星期四, 星期五, 星期六}; String hourTimeFormat = "HH:mm"; String monthTimeFormat = "M月d日 HH:mm"; String yearTimeFormat = "yyyy年M月d日 HH:mm"; try { Calendar todayCalendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timestamp); if (todayCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {//当年 if (todayCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)) {//当月 int temp = todayCalendar.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH); switch (temp) { case 0://今天 result = getTime(timestamp, hourTimeFormat); break; case 1://昨天 result = "昨天 " + getTime(timestamp, hourTimeFormat); break; case 2: case 3: case 4: case 5: case 6: int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); result = weekNames[dayOfWeek - 1] + " " + getTime(timestamp, hourTimeFormat); break; default: result = getTime(timestamp, monthTimeFormat); break; } } else { result = getTime(timestamp, monthTimeFormat); } } else { result = getTime(timestamp, yearTimeFormat); } return result; } catch (Exception e) { Log.e("getTimeString", e.getMessage()); return ""; } } public static String getTime(long time, String pattern) { Date date = new Date(time); return dateFormat(date, pattern); } public static String dateFormat(Date date, String pattern) { SimpleDateFormat format = new SimpleDateFormat(pattern); return format.format(date); }