java簡(jiǎn)單友好時(shí)間處理

第一次寫分享,這是主要是針對(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();

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容