如何在Android中更改日期时间语言 [英] How to change date time language in android

查看:113
本文介绍了如何在Android中更改日期时间语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android中更改日期/时间语言,而无需更改设备语言。

How can we change date/time language in android without changing the device language.

以下是我当前的代码。
以下代码根据设备语言进行更改。但是我想更改,而不更改设备语言设置

Below is my current code. below code changes according to the device language. but i want to change without changing the device language settings

public static String formatTime(Date time)
{
String timeFormat = UserSettingManager.getUserSetting(UserSettingManager.PREF_TIME_FORMAT);
if(StringUtils.isEmptyOrWhitespace(timeFormat))
{
timeFormat = DEFAULT_TIME_FORMAT;
}

public static String formatTime(Date time) { String timeFormat = UserSettingManager.getUserSetting(UserSettingManager.PREF_TIME_FORMAT); if(StringUtils.isEmptyOrWhitespace(timeFormat)) { timeFormat = DEFAULT_TIME_FORMAT; }


推荐答案

尝试这个:

public static String formatTime(Date time, Locale locale){
    String timeFormat = UserSettingManager
                           .getUserSetting(UserSettingManager.PREF_TIME_FORMAT);

    if(StringUtils.isEmptyOrWhitespace(timeFormat)){
        timeFormat = DEFAULT_TIME_FORMAT;
    }

    SimpleDateFormat formatter;

    try {
        formatter = new SimpleDateFormat(timeFormat, locale);            
    } catch(Exception e) {
        formatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT, locale);
    }
    return formatter.format(time);
}

然后像

Log.e("CHINESE DATE", formatTime(new Date(), Locale.CHINESE));



编辑



如果不在默认列表中找到一个区域设置,您可以使用其构造函数
实例化一个区域设置:

EDIT

If you don't find a locale in the default list you can instantiate one using its constructor :

Locale spanish = new Locale("es", "ES");

所以成为

Log.e("CHINESE DATE", formatTime(new Date(), new Locale("es", "ES"));

这篇关于如何在Android中更改日期时间语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆