Skip to content

Instantly share code, notes, and snippets.

@dingbuoyi
Created June 14, 2017 05:57
Show Gist options
  • Save dingbuoyi/b2917576974d8e1252990be71a2dc1ce to your computer and use it in GitHub Desktop.
Save dingbuoyi/b2917576974d8e1252990be71a2dc1ce to your computer and use it in GitHub Desktop.
计算时间的
public static String converTime(long timestamp) {
long currentSeconds = getFixedCurrentTime() / 1000;
long timeGap = currentSeconds - timestamp / 1000;// 与现在时间相差秒数
String timeStr = null;
if (timeGap > 24 * 60 * 60 * 365) {// 1天以上
// timeStr = timeGap / (24 * 60 * 60) + "天前";
timeStr = new SimpleDateFormat("yyyy年MM月dd日").format(new Date(timestamp));
} else if (timeGap > 24 * 60 * 60) {
timeStr = new SimpleDateFormat("MM月dd日").format(new Date(timestamp));
if (timeStr.startsWith("0")) {
timeStr = timeStr.substring(1);
}
} else if (timeGap > 60 * 60) {// 1小时-24小时
timeStr = timeGap / (60 * 60) + "小时前";
} else if (timeGap > 60) {// 1分钟-59分钟
timeStr = timeGap / 60 + "分钟前";
} else if (timeGap > 5 && timeGap <= 60) {// 1秒钟-59秒钟
timeStr = timeGap + "秒前";
} else {
timeStr = "刚刚";
}
return timeStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment