編輯:關於 Android 程式設計
做多語言版本時,日期與時間的格式往往最麻煩——同一個時間點,不同地區的呈現習慣完全不一樣。所幸 Android(以及 Java 標準庫)提供了 DateFormat,只要傳入目標語區的 Locale,它就會依當地慣例輸出。
public static CharSequence formatTimeInListForOverSeaUser(
final Context context, final long time, final boolean simple,
Locale locale) {
final GregorianCalendar now = new GregorianCalendar();
// 特殊時間,直接留空
if (time < MILLSECONDS_OF_HOUR) {
return "";
}
// 今天:只給時間
final GregorianCalendar today = new GregorianCalendar(
now.get(GregorianCalendar.YEAR),
now.get(GregorianCalendar.MONTH),
now.get(GregorianCalendar.DAY_OF_MONTH));
final long in24h = time - today.getTimeInMillis();
if (in24h > 0 && in24h <= MILLSECONDS_OF_DAY) {
java.text.DateFormat df = java.text.DateFormat.getTimeInstance(
java.text.DateFormat.SHORT, locale);
return "" + df.format(time);
}
// 昨天:加上「昨天」字樣,simple 時只顯示文字
final long in48h = time - today.getTimeInMillis() + MILLSECONDS_OF_DAY;
if (in48h > 0 && in48h <= MILLSECONDS_OF_DAY) {
return simple ? context.getString(R.string.fmt_pre_yesterday)
: context.getString(R.string.fmt_pre_yesterday) + " "
+ java.text.DateFormat.getTimeInstance(
java.text.DateFormat.SHORT, locale).format(time);
}
final GregorianCalendar target = new GregorianCalendar();
target.setTimeInMillis(time);
// 本週內:只顯示星期幾
if (now.get(GregorianCalendar.YEAR) == target.get(GregorianCalendar.YEAR)
&& now.get(GregorianCalendar.WEEK_OF_YEAR)
== target.get(GregorianCalendar.WEEK_OF_YEAR)) {
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("E", locale);
final String dow = "" + sdf.format(time);
return simple ? dow : dow + java.text.DateFormat.getTimeInstance(
java.text.DateFormat.SHORT, locale).format(time);
}
// 今年內:日期,或日期加時間
if (now.get(GregorianCalendar.YEAR) == target.get(GregorianCalendar.YEAR)) {
return simple ? java.text.DateFormat.getDateInstance(
java.text.DateFormat.SHORT, locale).format(time)
: java.text.DateFormat.getDateTimeInstance(
java.text.DateFormat.SHORT,
java.text.DateFormat.SHORT, locale).format(time);
}
// 跨年:完整日期
return simple ? java.text.DateFormat.getDateInstance(
java.text.DateFormat.SHORT, locale).format(time)
: java.text.DateFormat.getDateTimeInstance(
java.text.DateFormat.SHORT, java.text.DateFormat.SHORT,
locale).format(time);
}
這個函式的價值不在格式化本身,而在「依時間距離決定顯示精度」:一小時內不顯示、今天只給時分、昨天加字樣、本週只顯示星期、今年顯示日期、跨年顯示完整日期。這正是聊天與動態列表最常見的呈現策略,讓資訊密度隨時間遠近自然遞減。
這裡用的是 java.text.DateFormat。Android 另有一個 android.text.format.DateFormat,兩者同名但不同套件,後者無法指定 Locale,它只提供 format()、is24HourFormat() 這類 helpers。需要跟隨語區變化的場合,一定要 import 對的類別——程式中刻意寫成 java.text.DateFormat 全限定名稱,就是為了避免 IDE 自動 import 出錯。
API 說明可參考:https://developer.android.com/reference/java/text/DateFormat.html
第一,Locale 建議由使用者設定的語系推導,而不是直接寫死 Locale.getDefault();App 內提供語言切換時,記得把選定的 Locale 一路傳進來。
第二,simple 參數的語意是「要不要帶時間」,命名上較不直觀;若在專案裡重構,改名為 showTime 之類會更好讀。
新浪微博怎麼加好友?發現找人完整教學
新浪微博是全國最主流、最具人氣的微博產品。一句話隨意記錄生活,用手機隨時隨地發微博。微博同樣能和朋友聊私信,你可以關注想關注的人,了解他們的生活。那麼新浪微博該如何加好友
Android 分段錄影實作:仿美拍/糗事百科的拍攝流程
前陣子手邊事情比較多,部落格擱置了一段時間。這次我們來做一個美拍或糗事百科那種分段錄影的功能。 先釐清流程 1. 開啟相機;2. 開始錄製;3. 支援分段錄製,且每一段可
Android Studio 如何添加 aar 依賴
在 Android Studio 中引入本地 aar 檔案,其實只要四個步驟就能搞定。 1、把 aar 複製到專案的 libs 目錄 2、在 module 的 buil
揭開 RecyclerView 的神秘面紗(二):三種實戰處理點擊事件
前言 上一篇《揭開 RecyclerView 的神秘面紗(一):RecyclerView 的基本使用》中介紹了 RecyclerView 的基本用法,以及不同版面配置管理