Reputation: 1554
I want to show date format 01-01-2014 while selecting date from android date picker but i get in format 1-1-2014 due to this i get problem while searching date
here is my code
// 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);
}
};
how to format selected date in to this way 01-01-2014?
Upvotes: 1
Views: 5894
Reputation: 1354
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);
}
Upvotes: 3
Reputation: 1554
here i got alternative for accepted answer
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();
}
}
};
Upvotes: 2
Reputation: 158
String formattedDay = (String.valueOf(day));
String formattedMonth = (String.valueOf(month));
if(day < 10)
{
formattedDay = "0" + day;
}
if(month< 10)
{
formattedMonth = "0" + Month;
}
put this code right before StringBuilder Date;
and remove year = selectedYear;
month = selectedMonth;
day = selectedDay;
Then pass formattedDay
, formattedMonth
and year
to your StringBuilder.
Hope it works for you.
Upvotes: 4
Reputation: 122
Try this, you can set the dateformat to whatever you want, check the SimpleDateFormat JavaDoc for examples.
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String test = dateFormat.format(calendar.getTime());
Log.e("TEST", test);
Upvotes: 1