Date formatting based on user locale on android(基于android上用户区域设置的日期格式)
问题描述
我想根据用户区域设置显示出生日期.在我的应用程序中,我的字段之一是出生日期,目前的格式为 dd/mm/yyyy
.所以如果用户改变了他的语言环境,日期格式也应该相应地改变.任何指针或代码示例都会真正帮助我克服这个问题.
I want to display a date of birth based on the user locale. In my application, one of my fields is the 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.
推荐答案
你可以使用DateFormat 类,根据用户区域设置格式化日期.
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.
这篇关于基于android上用户区域设置的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:基于android上用户区域设置的日期格式


- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- UITextView 内容插图 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01