Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the current date in a DatePicker?

Tags:

I need to be able to choose date and time in my application It should either be done by using a dialog, or starting a new intent.

The way I have done it now, is that I have started a new intent, since I need to set both date and time.

My problem is that I need to set the current date on the Datepicker (This is possible with the Timepicker using setCurrentHour and setCurrentMinute) onCreate, and set that as a lower boundary.

I can't find any functions in the Android API for doing this with the DatePicker.

Anyone have a suggestion to solve this problem?

Thanks in advance!

Edit: Important: The DatePicker and TimePicker must be in the same dialog/intent

like image 724
Ikky Avatar asked Jun 23 '11 09:06

Ikky


People also ask

How can we get the current date using Datepicker in Android?

For OnDateSetListener typecast the getActivity method to OnDateSetListener. For the year, month, and dayOfMonth create an instance of calendar class and assign the year, month, and dayOfMonth to variables of type int. Pass year and month and dayOfMonth so that when the DatePickerDialog opens it has the current date.

How do I set the default time on Datepicker?

$(". selector"). datepicker( {defaultDate:"+6"} );

How do you use a Datepicker?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.


1 Answers

Straight to code

Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog =
    new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
dialog.show();
like image 108
Labeeb Panampullan Avatar answered Oct 18 '22 09:10

Labeeb Panampullan