我使用DatePicker选择日期,一切都很好。但是问题是格式化日期。
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append(" "));
// Month is 0 based, just add 1
}
根据代码,我正在输出:2016-8-22,但我的预期输出是2016-08-22(我想要这样的月份08)
谢谢您,Karthi。
使用SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
tvDisplayDate.setText(sdf.format(c.getTime()));
有关SimpleDateFormat的更多信息,请参见官方文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句