I use a DatePicker
widget in Android for the user to set a date, and want to get the date value when a confirm button is clicked, how can I do that?
Asked
Active
Viewed 1.1e+01k times
48
7 Answers
87
Try this:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();

Shihab Uddin
- 6,699
- 2
- 59
- 74

Onuray Sahin
- 4,093
- 4
- 33
- 34
-
1This doesn't seem to work for me when updating the date picker values in the UI. It gives me the value it started with – Gene Bo Apr 29 '14 at 21:19
-
2Right. Please try to set OnDateSetListener to datePicker. – Onuray Sahin Apr 30 '14 at 10:08
-
1This worked for me. I set these values like this String day = String.valueof(datePicker.getDayOfMonth(); It so far works perfectly because it converts the ints to strings in my case for inserts to a sql database. – natur3 Apr 08 '15 at 03:02
23
I manged to set the MinDate & the MaxDate programmatically like this :
final Calendar c = Calendar.getInstance();
int maxYear = c.get(Calendar.YEAR) - 20; // this year ( 2011 ) - 20 = 1991
int maxMonth = c.get(Calendar.MONTH);
int maxDay = c.get(Calendar.DAY_OF_MONTH);
int minYear = 1960;
int minMonth = 0; // january
int minDay = 25;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.create_account);
BirthDateDP = (DatePicker) findViewById(R.id.create_account_BirthDate_DatePicker);
BirthDateDP.init(maxYear - 10, maxMonth, maxDay, new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
if (year < minYear)
view.updateDate(minYear, minMonth, minDay);
if (monthOfYear < minMonth && year == minYear)
view.updateDate(minYear, minMonth, minDay);
if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
view.updateDate(minYear, minMonth, minDay);
if (year > maxYear)
view.updateDate(maxYear, maxMonth, maxDay);
if (monthOfYear > maxMonth && year == maxYear)
view.updateDate(maxYear, maxMonth, maxDay);
if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
view.updateDate(maxYear, maxMonth, maxDay);
}}); // BirthDateDP.init()
} // activity
it works fine for me, enjoy :)
-
i want to do opposite of it, means min date will be current date and max date will be any date greater than current date. How to do that. Any help will be highly appreciated.I tried but getting some problem.Have a look at this http://stackoverflow.com/questions/8675984/date-picker-with-max-and-minimum-date-in-ondatechanged-in-android-1-5 – mH16 Dec 30 '11 at 05:31
15
If you are using Kotlin, you can define an extension function for DatePicker
:
fun DatePicker.getDate(): Date {
val calendar = Calendar.getInstance()
calendar.set(year, month, dayOfMonth)
return calendar.time
}
Then, it's just: datePicker.getDate()
. As if it had always existed.

lcnicolau
- 3,252
- 4
- 36
- 53
5
you mean that you want to add DatePicker widget into your apps.
Global variable declaration into your activity class:
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
write down this code into onCreate() function:
//date picker presentation
mPickDate = (Button) findViewById(R.id.pickDate);//button for showing date picker dialog
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { showDialog(DATE_DIALOG_ID); }
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
write down those function outside of onCreate() function:
//return date picker dialog
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
//update month day year
private void updateDisplay() {
mBodyText.setText(//this is the edit text where you want to show the selected date
new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay).append(""));
//.append(mMonth + 1).append("-")
//.append(mDay).append("-")
//.append(mYear).append(" "));
}
// the call back received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};

rabby
- 335
- 2
- 2
-
Really a good one, you can also implement it if you require birthday input in an EditText – Syed Rafay Mar 22 '18 at 15:56
-
Please note: onCreateDialog is deprecated, you can create a DialogFragment to use – Joseph Wambura Jan 27 '19 at 09:21
4
you can also use this code...
datePicker = (DatePicker) findViewById(R.id.schedule_datePicker);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
Date d = new Date(year, month, day);
String strDate = dateFormatter.format(d);

Manigandan P.S
- 170
- 2
- 11
1
U can also use te Calendar.GregorianCalendar java class
GregorianCalendar calendarBeg=new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),datePicker.getDayOfMonth());
Date begin=calendarBeg.getTime();

Pilot Alpal
- 43
- 6
1
The DatePicker class has methods for getting the month, year, day of month. Or you can use an OnDateChangedListener.

kgiannakakis
- 103,016
- 27
- 158
- 194