Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change date time language in 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; }

SimpleDateFormat formatter; try { formatter = new SimpleDateFormat(timeFormat); } catch(Exception e) { formatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT); } return formatter.format(time); }
like image 835
Syed Avatar asked Jun 30 '15 10:06

Syed


1 Answers

try this:

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);
}

And then like

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");

So it becomes

Log.e("SPANISH DATE", formatTime(new Date(), new Locale("es", "ES"));
like image 133
Carlo Moretti Avatar answered Nov 14 '22 06:11

Carlo Moretti