我试图根据用户的设置获取格式化的日期(年、月、日)和时间(小时、分钟、秒)字符串。This https://groups.google.com/forum/#!topic/android-developers/I35vKNWqLdQAndroid 开发者 Google 群组中的帖子描述了我遇到的确切问题,但没有人帮助该人解决它。我总结一下:
Android 有这些类尝试执行上述操作,但没有一个类同时使用用户首选项并显示秒数。
java.text.DateFormat
不使用“设置”应用中设置的首选项
android.text.format.DateFormat
Gets a java.text.DateFormat
使用正确格式化输出的对象getDateFormat()
and getTimeFormat()
, but getTimeFormat()
不包括秒。
android.text.format.DateUtils
不使用在“设置”应用程序中显示日期的首选项设置,也无法显示秒。
例如,将“设置”中的首选项设置为 DD/MM/YYYY 且 24 小时格式 = 打开,运行以下代码:
long timestamp=System.currentTimeMillis();
sometextview.setText(
java.text.format.DateFormat.getDateTimeInstance().format(new Date(timestamp))
+"\n"+
android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp))
+" "+
android.text.format.DateFormat.getTimeFormat(this).format(new Date(timestamp))
+"\n"+
android.text.format.DateUtils.formatDateTime(this,
timestamp,
DateUtils.FORMAT_SHOW_DATE|
DateUtils.FORMAT_SHOW_TIME|
DateUtils.FORMAT_SHOW_YEAR)
);
在文本视图中给出以下输出:
Apr 27,2014 5:47:18 PM
27/04/2014 17:47
April 27,2014, 17:47
没有一个给出所需的输出,使用上述首选项将是这样的:
27/04/2014 17:47:18
我已经调查过乔达时间安卓 https://github.com/dlew/joda-time-android图书馆也是如此,但它似乎没有做我需要的事情(如果我错了,请纠正我)。
TL;DR:如何根据用户偏好设置日期/时间格式有秒在安卓上?