如何在不更改设备语言的情况下更改android中的日期/时间语言。
以下是我目前的代码。 下面的代码根据设备语言而变化。但我想在不更改设备语言设置的情况下进行更改
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);
}
答案 0 :(得分:6)
试试这个:
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));
如果您未在默认列表中找到区域设置,则可以使用其构造函数对其进行实例化 :
Locale spanish = new Locale("es", "ES");
所以它变成
Log.e("CHINESE DATE", formatTime(new Date(), new Locale("es", "ES"));
答案 1 :(得分:0)
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));

答案 2 :(得分:0)
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));