在我简单的android应用中,显示当前日期为2014年5月8日。使用此代码
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = sdf.format(new Date());
datepick.setText(currentDate);
当用户想要更改日期时,我将DatePicker用于该textview(datepick)。使用此代码
//To show current date in the datepicker
Calendar mcurrentDate=Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpdialog=new DatePickerDialog(QAOD_Tab_1.this, new OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int selectedyear, int selectedmonth, int selectedday) {
datepick.setText(new StringBuilder()
.append(selectedday).append("-").append(selectedmonth + 1).append("-")
.append(selectedyear).append(" "));
}
}, mYear, mMonth, mDay);
dpdialog.setTitle("Select Date");
dpdialog.show(); `
现在正在获得类似2014年5月8日的日期。我还需要在日期和月份之前显示零。如何以这种格式显示零。我需要像这样的结果2014年5月8日。
在日期设置中,如果您使用的是api级别12及更高版本,则可以使用以下代码
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String selectedDate = sdf.format(view.getCalendarView().getDate());
您的api级别小于12,则必须使用字符串进行操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句