unix时间戳是秒级时间戳
date需要毫秒级时间戳
在配上格式化字符串与Calendar
完成时间戳的转换
<string name="time_format">%1$d年%2$d月%3$d日%4$d时%5$d分</string>
public static String timeStamp2Date(long timestamp) {
timestamp = timestamp * 1000;
Date date = new Date(timestamp);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Resources resources = GlobalContext.getContext().getResources();
String suffix = String.format(resources.getString(R.string.time_format), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
return resources.getString(R.string.disallowcomment_prefix) + suffix;
}
或者
new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(date)
第二种方法会带0
2018年05月20日
第一种不会