Syed
Syed

Reputation: 287

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

Upvotes: 6

Views: 10741

Answers (3)

Carlo Moretti
Carlo Moretti

Reputation: 2250

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

Upvotes: 8

Kunkun Liu
Kunkun Liu

Reputation: 81

 java.text.DateFormat shortDateFormat = DateFormat.getDateFormat(context)
  final Calendar now = Calendar.getInstance()
   mDummyDate.setTimeZone(now.getTimeZone())
  // We use December 31st because it's unambiguous when demonstrating the date format
 // We use 13:00 so we can demonstrate the 12/24 hour options
 mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
 Date dummyDate = mDummyDate.getTime();
 mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime()));
mTimeZone.setSummary(getTimeZoneText(now.getTimeZone()));
mDatePref.setSummary(shortDateFormat.format(now.getTime()));
mDateFormat.setSummary(shortDateFormat.format(dummyDate));
mTime24Pref.setSummary(DateFormat.getTimeFormat(gtActivity()).format(dummyDate));

Upvotes: 0

Kunkun Liu
Kunkun Liu

Reputation: 81

 java.text.DateFormat shortDateFormat = DateFormat.getDateFormat(context)
  final Calendar now = Calendar.getInstance()
   mDummyDate.setTimeZone(now.getTimeZone())
  // We use December 31st because it's unambiguous when demonstrating the date format
 // We use 13:00 so we can demonstrate the 12/24 hour options
 mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
 Date dummyDate = mDummyDate.getTime();
 mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime()));
mTimeZone.setSummary(getTimeZoneText(now.getTimeZone()));
mDatePref.setSummary(shortDateFormat.format(now.getTime()));
mDateFormat.setSummary(shortDateFormat.format(dummyDate));
mTime24Pref.setSummary(DateFormat.getTimeFormat(gtActivity()).format(dummyDate));

Upvotes: 0

Related Questions