消息时间展示效果(仿微信聊天消息时间展示)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class GetTimeStringUtils {
public static String getTimeString(Long timestamp) {
String result = "";
String[] 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) {
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);
}
public static long timeToStamp(Date timers) {
Date d = new Date();
long timeStemp = 0;
try {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = timers.toString();
d = sf.parse(s);// 日期转换为时间戳
} catch (ParseException e) {
e.printStackTrace();
}
timeStemp = d.getTime();
return timeStemp;
}
}
代码已订正,可直接运行使用。欢迎不同意见和优秀建议!
