在阅读了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。根据{{3}},适当的本地化德国日期格式是dd.mm.yyyy,所以我希望看到" 26.02.1974"。
这导致了我的问题,是否有办法完全本地化日期,或者这是一个手动过程,我必须通过我的应用程序了解日期,时间等?
答案 0 :(得分:2)
String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (Exception e) {
// handle exception here !
}
// get localized date formats
Log.i(this,"sdf default: "+new SimpleDateFormat().format(date)); // using my phone locale
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
Log.i(this,"dateFormat US DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
Log.i(this,"dateFormat US SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE SHORT: "+dateFormat.format(date));
输出是:
sdf default: 26.02.74 0:00
dateFormat US DEFAULT: Feb 26, 1974
dateFormat GERMAN DEFAULT: 26.02.1974
dateFormat CHINESE DEFAULT: 1974-2-26
dateFormat US SHORT: 2/26/74
dateFormat GERMAN SHORT: 26.02.74
dateFormat CHINESE SHORT: 74-2-26