Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 日期時間國際化:用 DateFormat 依語區自動格式化

編輯:關於 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 之類會更好讀。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved