我正在研究日历日期选择器。我正在获取格式为2013/7/7
的日期,但我希望格式为2013/07/07
码:
protected Dialog onCreateDialog(int id)
{
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
}
private DatePickerDialog.OnDateSetListener mDateSetListene = new DatePickerDialog.OnDateSetListener()
{
// onDateSet method
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date_selected = String.valueOf(year)+" /"+String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth);
dob.setText(date_selected);
}
};
最佳答案
String strDate="2013/7/7";
String[] strsplit=strDate.split("/");
int year=Integer.parseInt(strsplit[0]);
int month=Integer.parseInt(strsplit[1]);
int day=Integer.parseInt(strsplit[2]);
if((month)<=9)
strDate+="0";
strDate+=(month )+"/";
if(day<=9)
strDate+="0";
strDate+=(day)+"/";
strDate+=(year);