【发布时间】:2015-06-14 00:13:36
【问题描述】:
在阅读了接受德语的Date formatting based on user locale on android 的答案后,我测试了以下内容:
@Override
protected void onResume() {
super.onResume();
String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (ParseException e) {
// handle exception here !
}
// get localized date formats
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String s = dateFormat.format(date);
dateTV.setText(s);
}
这里的 dateOfBirth 是一个英文日期。但是,如果我将手机的语言更改为德语,我会看到 02.26.1974。根据http://en.wikipedia.org/wiki/Date_format_by_country,正确的本地化德语日期格式是dd.mm.yyyy,所以我希望看到“26.02.1974”。
这引出了我的问题,有没有办法完全本地化日期,或者这是一个手动过程,我必须在我的应用程序中仔细查看日期、时间等?
【问题讨论】:
-
但是您自己在
new SimpleDateFormat("MM/dd/yyyy");中定义了一种格式,格式化日期结果的唯一区别是使用点而不是斜线。此外,您没有在代码中的任何地方使用 Locale。 -
@Stan getDateFormat 返回本地化格式:developer.android.com/reference/android/text/format/…。我正在尝试找到可以“完全”本地化的代码,重新排列数字,而不仅仅是在这种情况下用点替换。
标签: android date localization