我有一个日历对象,我知道如何使用android.text.format()格式化它
如何获取日期和时间的电话设置以将对象格式化为用户设置的格式。就像24小时制,年-月-日…

最佳答案

我想要的是在单独的文本视图中提供根据用户偏好和区域设置格式化的日期和时间。
我使用DateFormat类中的静态方法完成了这项工作:

DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); // Gets system date format
DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(getApplicationContext()); // Gets system time format

txt1.setText(dateFormat.format(CalendarInstance.getTime())); // format() Returns a string according to the user's preferences (12-hr vs 24-hr) and locale
txt2.setText(timeFormat.format(CalendarInstance.getTime()));

09-11 19:12
查看更多