pheromix
pheromix

Reputation: 19347

How to make DatePickerDialog select a particular date?

When an EditText's content is empty then the DatePickerDialog is showing current date :

Calendar calendrier = Calendar.getInstance();
DatePickerDialog picker = new DatePickerDialog(ctxt, evt, calendrier.get(Calendar.YEAR), calendrier.get(Calendar.MONTH), calendrier.get(Calendar.DAY_OF_MONTH));

But I want the DatePickerDialog showing a date based on the EdiText's content when this content is not empty. How to do that ?

Upvotes: 0

Views: 437

Answers (3)

BBdev
BBdev

Reputation: 4942

Yes you can easily do it with updateDate(int year, int month, int dayOfMonth) method of DatePicker where you have to pass the date, month and year as a parameter.

Upvotes: 1

Fahid Nadeem
Fahid Nadeem

Reputation: 412

You can first get the string from your edittext and then convert it to date (check this http://www.mkyong.com/java/how-to-convert-string-to-date-java/ for convertion.) Then get the year,month,day from the date object and pass it to your datepicker when showing picker like this

datePicker.init(year, month, day, null);

Here you go. Cheers!!

Upvotes: 1

Héctor Valls
Héctor Valls

Reputation: 26084

It depends on the format that user puts the date in the EditText. For instance, if format is dd/MM/yyyy then:

SimpleDateFormat formatter =
        new SimpleDateFormat("dd/MM/yyyy");

Date date = formatter.parse(eddittext.getText().toString());
Calendar calendrier = Calendar.getInstance();
calendrier.setTime(date);

DatePickerDialog picker = new DatePickerDialog(ctxt, evt, calendrier.get(Calendar.YEAR), calendrier.get(Calendar.MONTH), calendrier.get(Calendar.DAY_OF_MONTH));

This assumes that the user puts the data in the correct format.

Upvotes: 1

Related Questions