Android 時間轉換工具類
阿新 • • 發佈:2018-11-14
在做聊天頁面的時候,難免遇到要把一個時間戳轉換為一個好看的格式,每次都動手寫也太累了,在這裡給大家分享一下我的工具類吧
其中time_year、time_month等 在文字最後貼出來給大家哦。
/** * 時間轉換工具 */ public class TimeUtil { private TimeUtil(){} /** * 時間轉化為顯示字串 * * @param timeStamp 單位為秒 */ public static String getTimeStr(long timeStamp){ if (timeStamp==0) return ""; Calendar inputTime = Calendar.getInstance(); inputTime.setTimeInMillis(timeStamp*1000); Date currenTimeZone = inputTime.getTime(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); if (calendar.before(inputTime)){ //今天23:59在輸入時間之前,解決一些時間誤差,把當天時間顯示到這裡 SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + AppApplication.getContext().getResources().getString(R.string.time_year)+"MM"+AppApplication.getContext().getResources().getString(R.string.time_month)+"dd"+AppApplication.getContext().getResources().getString(R.string.time_day)); return sdf.format(currenTimeZone); } calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (calendar.before(inputTime)){ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); return sdf.format(currenTimeZone); } calendar.add(Calendar.DAY_OF_MONTH,-1); if (calendar.before(inputTime)){ return AppApplication.getContext().getResources().getString(R.string.time_yesterday); }else{ calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.MONTH, Calendar.JANUARY); if (calendar.before(inputTime)){ SimpleDateFormat sdf = new SimpleDateFormat("M"+AppApplication.getContext().getResources().getString(R.string.time_month)+"d"+AppApplication.getContext().getResources().getString(R.string.time_day)); return sdf.format(currenTimeZone); }else{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + AppApplication.getContext().getResources().getString(R.string.time_year)+"MM"+AppApplication.getContext().getResources().getString(R.string.time_month)+"dd"+AppApplication.getContext().getResources().getString(R.string.time_day)); return sdf.format(currenTimeZone); } } } /** * 時間轉化為聊天介面顯示字串 * * @param timeStamp 單位為秒 */ public static String getChatTimeStr(long timeStamp){ if (timeStamp==0) return ""; Calendar inputTime = Calendar.getInstance(); inputTime.setTimeInMillis(timeStamp*1000); Date currenTimeZone = inputTime.getTime(); Calendar calendar = Calendar.getInstance(); if (calendar.before(inputTime)){ //當前時間在輸入時間之前 SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + AppApplication.getContext().getResources().getString(R.string.time_year)+"MM"+AppApplication.getContext().getResources().getString(R.string.time_month)+"dd"+AppApplication.getContext().getResources().getString(R.string.time_day)); return sdf.format(currenTimeZone); } calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (calendar.before(inputTime)){ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); return sdf.format(currenTimeZone); } calendar.add(Calendar.DAY_OF_MONTH,-1); if (calendar.before(inputTime)){ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); return AppApplication.getContext().getResources().getString(R.string.time_yesterday)+" "+sdf.format(currenTimeZone); }else{ calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.MONTH, Calendar.JANUARY); if (calendar.before(inputTime)){ SimpleDateFormat sdf = new SimpleDateFormat("M"+AppApplication.getContext().getResources().getString(R.string.time_month)+"d"+AppApplication.getContext().getResources().getString(R.string.time_day)+" HH:mm"); return sdf.format(currenTimeZone); }else{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+AppApplication.getContext().getResources().getString(R.string.time_year)+"MM"+AppApplication.getContext().getResources().getString(R.string.time_month)+"dd"+AppApplication.getContext().getResources().getString(R.string.time_day)+" HH:mm"); return sdf.format(currenTimeZone); } } } }
資原始檔引用 存放至 res - values - strings.xml 檔案中即可使用喲
<string name="time_day">日</string>
<string name="time_month">月</string>
<string name="time_year">年</string>