以友好的方式展示时间
package com.industry.base.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 以友好的方式展示时间
*
* @author z
* @date 2018/3/31 13:53
*/
public class TimeFriendlyUtil {
private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private static final int SEC = 1000;
private static final int MIN = 60000;
private static final int HOUR = 3600000;
private static final int DAY = 86400000;
/**
* 以友好的方式显示时间
*
* @param dateTime 时间
* @return 友好的时间格式
*/
public static String friendlyTime(Date dateTime) {
if (dateTime == null) {
return "Unknown";
}
if (System.currentTimeMillis() > dateTime.getTime()) {
return friendlyTimeBefore(dateTime);
}
return friendlyTimeAfter(dateTime);
}
/**
* 以友好的方式显示时间(yyyy-MM-dd)
*
* @param dateTime 时间
* @return 有好的时间表达格式
*/
public static String friendlyTimeBefore(Date dateTime) {
if (dateTime == null) {
return "Unknown";
}
String ftime = "";
Calendar cal = Calendar.getInstance();
long lt = dateTime.getTime();
long ct = cal.getTimeInMillis();
int days = DateUtil.daysBetween(dateTime, new Date());
if (days == 0) {
int hour = (int) ((cal.getTimeInMillis() - dateTime.getTime()) / 3600000);
if (hour == 0) {
ftime = Math.max((cal.getTimeInMillis() - dateTime.getTime()) / 60000, 1) + "分钟前";
} else {
ftime = "今天" + DateUtil.date2String(dateTime, "HH:mm");
}
} else if (days == 1) {
ftime = "昨天" + DateUtil.date2String(dateTime, "HH:mm");
} else if (days > 1) {
ftime = dateFormater2.get().format(dateTime);
}
return ftime;
}
/**
* 以友好的方式显示时间(之后)
*
* @param dateTime 时间
* @return 友好的方式显示时间
*/
public static String friendlyTimeAfter(Date dateTime) {
if (dateTime == null) {
return "Unknown";
}
String ftime = "";
Calendar cal = Calendar.getInstance();
// 判断是否是同一天
String curDate = dateFormater2.get().format(cal.getTime());
String paramDate = dateFormater2.get().format(dateTime);
if (curDate.equals(paramDate)) {
int hour = (int) ((dateTime.getTime() - cal.getTimeInMillis()) / 3600000);
if (hour == 0) {
ftime = Math.max((dateTime.getTime() - cal.getTimeInMillis()) / 60000, 1) + "分钟后";
} else {
ftime = hour + "小时后";
}
return ftime;
}
long lt = dateTime.getTime() / 86400000;
long ct = cal.getTimeInMillis() / 86400000;
int days = (int) (lt - ct);
if (days == 0) {
int hour = (int) ((dateTime.getTime() - cal.getTimeInMillis()) / 3600000);
if (hour == 0) {
ftime = Math.max((dateTime.getTime() - cal.getTimeInMillis()) / 60000, 1) + "分钟后";
} else {
ftime = hour + "小时后";
}
} else if (days == 1) {
ftime = "明天";
} else if (days == 2) {
ftime = "后天";
} else if (days > 2 && days <= 10) {
ftime = days + "天后";
} else if (days > 10) {
ftime = dateFormater2.get().format(dateTime);
}
return ftime;
}
/**
* 友好时间
*
* @param millis 时间戳
* @return 友好时间
*/
public static String getFriendlyTimeSpanByNow(long millis) {
long now = System.currentTimeMillis();
long span = now - millis;
if (span < 0) {
// U can read http://www.apihome.cn/api/java/Formatter.html to understand it.
return String.format("%tc", millis);
}
if (span < SEC) {
return "刚刚";
} else if (span < MIN) {
return String.format(Locale.getDefault(), "%d秒前", span / SEC);
} else if (span < HOUR) {
return String.format(Locale.getDefault(), "%d分钟前", span / MIN);
}
// 获取当天 00:00
long wee = getWeeOfToday();
if (millis >= wee) {
return String.format("今天%tR", millis);
} else if (millis >= wee - DAY) {
return String.format("昨天%tR", millis);
} else {
return String.format("%tF", millis);
}
}
/**
* 获取当天 00:00
*
* @return 当天 00:00时间戳
*/
private static long getWeeOfToday() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}
}