我想在从android日期选择器中选择日期时显示日期格式01-01-2014 但我得到格式1-1-2014,因为我在搜索日期时遇到问题
这是我的代码
// On click Of Button
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAESubmit:
saveData();
break;
case R.id.lblAEDate:
showDialog(AE_DT_DIALOG_ID);
break;
}
}
// to create Date dialog
protected Dialog onCreateDialog(int id) {
switch (id) {
case AE_DT_DIALOG_ID:
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, yy, mm, dd);
}
return null;
}
// DAte Listener Call on date dialog call
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
StringBuilder Date;
Date = new StringBuilder().append(day).append("-").append(month + 1).append("-").append(year).append(" ");
// set selected date into lbl View
lblAEDate.setText(Date);
}
};
如何格式化所选日期01-01-2014?
答案 0 :(得分:4)
String formattedDay = (String.valueOf(day));
String formattedMonth = (String.valueOf(month));
if(day < 10)
{
formattedDay = "0" + day;
}
if(month< 10)
{
formattedMonth = "0" + Month;
}
将此代码放在StringBuilder Date;
之前,然后移除year = selectedYear;
month = selectedMonth;
day = selectedDay;
然后将formattedDay
,formattedMonth
和year
传递给您的StringBuilder。
希望它适合你。
答案 1 :(得分:3)
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
}
public void populateSetDate(int year, int month, int day) {
String formattedDay = (String.valueOf(day));
String formattedMonth = (String.valueOf(month));
if (day < 10) {
formattedDay = "0" + day;
}
if (month < 10) {
formattedMonth = "0" + month;
}
btnDate.setText(formattedDay + "-" + formattedMonth + "-" + year);
}
答案 2 :(得分:2)
这里我接受了接受的答案
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
String dt;
year = selectedYear;
month = selectedMonth + 1;
day = selectedDay;
try {
dt = day + "-" + month + "-" + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); // Set your date format
Date currentDt = null;
currentDt = sdf.parse(dt);
String d = sdf.format(currentDt);
// set selected date into lbl View
lblDate.setText(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
答案 3 :(得分:1)
试试这个,您可以将日期格式设置为您想要的任何内容,请查看SimpleDateFormat JavaDoc以获取示例。
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String test = dateFormat.format(calendar.getTime());
Log.e("TEST", test);