最近公司项目在搞动态相关的,产品昨天给出了时间格式,下午花了一点时间搞了一下,分享给大家。
1 分钟以内:刚刚
1-2分钟:1分钟前
过了1个小时(60分钟以内使用xx分钟前):1小时前
过了1个24:00:昨天
过了2个24:00:2天前
过了7个24:00:xx月xx日
不是本年:xxxx年xx月xx日
public class TimeHelp {
public static String format(long timeMillis) {
return format(new Date(timeMillis));
}
private static String format(Date date) {
Calendar calendar = Calendar.getInstance();
//当前年
int currYear = calendar.get(Calendar.YEAR);
//当前日
int currDay = calendar.get(Calendar.DAY_OF_YEAR);
//当前时
int currHour = calendar.get(Calendar.HOUR_OF_DAY);
//当前分
int currMinute = calendar.get(Calendar.MINUTE);
//当前秒
int currSecond = calendar.get(Calendar.SECOND);
calendar.setTime(date);
int msgY