日期格式基于在Android用户区域区域、日期、格式、用户

2023-09-13 00:40:01 作者:甜味拾荒者

我想显示出生基于用户区域设置日期。在我的应用我的领域​​之一,是出生日期这是目前在格式DD / MM / YYYY。因此,如果用户改变他的区域设置日期的格式也应相应地改变。任何指针或code样品将真正帮助我克服这个问题。

I want to display date of birth based on the user locale. In my application one of my field is date of birth which is currently in the format dd/mm/yyyy. So if the user changes his locale the date format should also change accordingly. Any pointers or code samples would really help me to overcome the problem.

推荐答案

您可以使用的日期格式类,根据用户区域格式化的日期。

You can use the DateFormat class that formats a date according to the user locale.

例如:

String dateOfBirth = "26/02/1974";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
    date = sdf.parse(dateOfBirth);
} catch (ParseException e) {
    // handle exception here !
}
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
String s = dateFormat.format(date);

您可以使用不同的方法 getLongDateFormat getMediumDateFormat 因冗长的水平,你想有。

You can use the different methods getLongDateFormat, getMediumDateFormat depending on the level of verbosity you would like to have.

 
精彩推荐