我试图在android中显示使用DatePicker选择的年,月和日的日期.我遇到的问题是选择年份,月份和日期并在屏幕上以正确的手机语言格式显示它们,而不显示时间.
现在的代码将正确显示日期,但也会显示时间.
StringBuilder string = new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(string.toString());
mDateDisplay.setText(date.toLocaleString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
mDateDisplay是一个TextView.
我无法弄清楚如何摆脱同时显示的时间.我做了大量的搜索和阅读文档,我无法理解.任何帮助,将不胜感激.
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getContext());
mDateDisplay.setText(dateFormat.format(date))
Run Code Online (Sandbox Code Playgroud)
这将考虑用户区域设置并仅显示日期.
以下是相关文档:getDateFormat(Context context)
| 归档时间: |
|
| 查看次数: |
12449 次 |
| 最近记录: |