Skip to content

Instantly share code, notes, and snippets.

@TakWolf
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakWolf/f1f083f69857ff283cb1 to your computer and use it in GitHub Desktop.
Save TakWolf/f1f083f69857ff283cb1 to your computer and use it in GitHub Desktop.
根据时间返回“几分钟”、“几小时”、“几天”的时间String
package com.takwolf.util;
import java.util.Date;
public class TimeUtil {
private final static long minute = 60 * 1000;// 分钟
private final static long hour = 60 * minute;// 小时
private final static long day = 24 * hour;// 天
private final static long week = 7 * day; // 周
private final static long month = 31 * day;// 月
private final static long year = 12 * month;// 年
public static String getTimeFormatText(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
if (diff > year) {
r = (diff / year);
return r + "年前";
}
if (diff > month) {
r = (diff / month);
return r + "个月前";
}
if (diff > week) {
r = (diff / week);
return r + "周前";
}
if (diff > day) {
r = (diff / day);
return r + "天前";
}
if (diff > hour) {
r = (diff / hour);
return r + "小时前";
}
if (diff > minute) {
r = (diff / minute);
return r + "分钟前";
}
return "刚刚";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment