Home>

Language change for "DateUtils.formatDateTime" is not applied only to Android4.1.2 (API16).
Note that Android 4.4.4, 7, and 8 will function without problems in actual machine tests.

Applicable source code
// Change the language of English language other than English to UK
        boolean localeChangeFlg = false;
        if ((Globals.getAppContext (). getString (R.string.language_str) .equals ("english"))&&locale.getDefault (). getLanguage (). equals (Locale.ENGLISH.getLanguage ()) == false) {
            defLocale = Locale.getDefault ();
            Locale locale = Locale.UK;
            Locale.setDefault (locale);
            Configuration config = new Configuration ();
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
                config.setLocale (locale);
                config.setLayoutDirection (locale);
            } else {
                config.locale = locale;
            }
            Globals.getAppContext (). GetResources (). UpdateConfiguration (config, null);
            localeChangeFlg = true;
        }
        // Display year/month/day
        cl_now = Calendar.getInstance ();
        String wkDate;
        if (Build.VERSION.SDK_INT>16) {
            wkDate = DateUtils.formatDateTime (this, cl_now.getTimeInMillis (), FORMAT_SHOW_YEAR | FORMAT_SHOW_DATE | FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL);
        } else {
            if (getString (R.string.language_str) .equals ("japanese")) {
                wkDate = DateUtils.formatDateTime (this, cl_now.getTimeInMillis (), FORMAT_SHOW_YEAR | FORMAT_SHOW_DATE);
                wkDate = wkDate + getString (R.string.stParenthesis) + DateUtils.formatDateTime (this, cl_now.getTimeInMillis (), FORMAT_SHOW_WEEKDAY | FORMAT_ABBREV_ALL) + getString (R.string.edParenthesis);
            } else {
                wkDate = DateUtils.formatDateTime (this, cl_now.getTimeInMillis (), FORMAT_SHOW_YEAR | FORMAT_SHOW_DATE | FORMAT_SHOW_WEEKDAY);
            }
        }
        TextView t_dateAndWeek = findViewById (R.id.t_dateAndWeek);
        t_dateAndWeek.setText (wkDate);
        // If the language is changed, restore it
        if (localeChangeFlg) {
            Locale locale = defLocale;
            Locale.setDefault (locale);
            Configuration config = new Configuration ();
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
                config.setLocale (locale);
                config.setLayoutDirection (locale);
            } else {
                config.locale = locale;
            }
            Globals.getAppContext (). GetResources (). UpdateConfiguration (config, null);
        }

Do not use "Locale.ENGLISH" instead of "Locale.UK"

Supplemental information (FW/tool version etc.)

Android Studio3.4
API level 14 to 28

  • Answer # 1

    If you look at the implementation of DateUtils.formatDateTime as of API16, it seems that the format language is determined based on the resources acquired with Resources.getSystem (). So, please add processing to rewrite the Configuration of Resources.getSystem () as follows.

    Resources systemRes = Resources.getSystem ();
    Configuration systemConfig = new Configuration (systemRes.getConfiguration ());
    if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
        systemConfig.setLocale (locale);
        systemConfig.setLayoutDirection (locale);
    } else {
        systemConfig.locale = locale;
    }
    systemRes.updateConfiguration (systemConfig, systemRes.getDisplayMetrics ());