第一次寫分享,這是主要是針對(duì)知識(shí)的總結(jié)
這是友好時(shí)間工具類、包括展示友好時(shí)間、判斷是否同一天、時(shí)間增加年、月、日
話不多說直接上代碼、如有問題直接評(píng)論、或者郵件溝通
public class FriendlyDateUtil {
/**
* 獲取友好時(shí)間
*
? ? * @param date 時(shí)間
? ? * @param isAf 是否顯示將來時(shí)間
*/
? ? public static String getFriendlyTime(Date date,boolean isAf) {
if (date ==null) {
return "";
}
SimpleDateFormat dateFormat;
int ct = (int) ((System.currentTimeMillis() - date.getTime()) /1000);
if (isAf) {
if (ct <0) {
if (ct < -86400) {//86400 * 30
? ? ? ? ? ? ? ? ? ? int day = ct / (-86400);
if (day ==1) {
return "后天 " +new SimpleDateFormat("HH:mm").format(date);
}
if (date.getYear() ==new Date().getYear()) {
dateFormat =new SimpleDateFormat("MM-dd HH:mm");
}else {
dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
return dateFormat.format(date);
}
if (!isSameDate(new Date(), date)) {//判斷跨天
? ? ? ? ? ? ? ? ? ? return "明天 " +new SimpleDateFormat("HH:mm").format(date);
}
if (-86400 < ct && ct < -3600) {
return String.format("%d小時(shí)后? %s", ct / -3600,new SimpleDateFormat("HH:mm").format(date));
}
if (-3600 < ct && ct < -61) {
return String.format("%d分鐘后? %s", Math.max(ct / -60,3),new SimpleDateFormat("HH:mm").format(date));
}
if (-61 < ct && ct <0) {
return String.format("即將到點(diǎn)? %s",new SimpleDateFormat("HH:mm").format(date));
}
}
}
if (ct <61) {//1分鐘內(nèi)
? ? ? ? ? ? return "剛剛";
}
dateFormat =new SimpleDateFormat("HH:mm");
if (isSameDate(new Date(), date)) {//
? ? ? ? ? ? if (ct <3600) {//1小時(shí)內(nèi)
? ? ? ? ? ? ? ? return String.format("%d分鐘前", Math.max(ct /60,3));
}
if (ct >=3600 && ct <86400) {//當(dāng)天超過一小時(shí)顯示
? ? ? ? ? ? ? ? return dateFormat.format(date);
}
}else {
//不是當(dāng)天時(shí)進(jìn)入
? ? ? ? ? ? if (ct <86400) {
return "昨天 " + dateFormat.format(date);
}
if (ct >=86400 && ct <259200) {//86400 * 3 (三天)
? ? ? ? ? ? ? ? int day = ct /86400;
if (!isSameDate(addDay(new Date(), -day), date)) {//判斷時(shí)間匹配日期
? ? ? ? ? ? ? ? ? ? day = day +1;
}
if (day ==1) {
return "昨天 " + dateFormat.format(date);
}else {
dateFormat =new SimpleDateFormat("MM-dd HH:mm");
return dateFormat.format(date);
}
}
}
if (date.getYear() ==new Date().getYear()) {
dateFormat =new SimpleDateFormat("MM-dd HH:mm");
}else {
dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
return dateFormat.format(date);
}
/**
* 是否兩個(gè)時(shí)間是否為同一天
*
? ? * @param date1,date2 兩個(gè)對(duì)比時(shí)間
*/
? ? private static boolean isSameDate(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
.get(Calendar.YEAR);
boolean isSameMonth = isSameYear
&& cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
boolean isSameDate = isSameMonth
&& cal1.get(Calendar.DAY_OF_MONTH) == cal2
.get(Calendar.DAY_OF_MONTH);
return isSameDate;
}
/**
* 天數(shù)增加
*
? ? * @param date 時(shí)間
? ? * @param day? 增加天數(shù) 可為負(fù)整數(shù)、正整數(shù)
*? ? ? ? ? ? 例如-1 相當(dāng)于減少一天
*? ? ? ? ? ? 1 相當(dāng)于增加一天
*/
? ? public static Date addDay(Date date,int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
return calendar.getTime();
}
/**
* 月數(shù)增加
*
? ? * @param date? 時(shí)間
? ? * @param month 增加月數(shù) 可為負(fù)整數(shù)、正整數(shù)
*? ? ? ? ? ? ? 例如-1 相當(dāng)于減少一月
*? ? ? ? ? ? ? 1 相當(dāng)于增加一月
*/
? ? public static Date addMonth(Date date,int month) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, month);
return calendar.getTime();
}
/**
* 年數(shù)增加
*
? ? * @param date 時(shí)間
? ? * @param year 增加年數(shù) 可為負(fù)整數(shù)、正整數(shù)
*? ? ? ? ? ? 例如-1 相當(dāng)于減少一年
*? ? ? ? ? ? 1 相當(dāng)于增加一年
*/
? ? public static Date addYear(Date date,int year) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, year);
return calendar.getTime();
}
}