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?
This question is related to
android
datepicker
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.
U can also use te Calendar.GregorianCalendar java class
GregorianCalendar calendarBeg=new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),datePicker.getDayOfMonth());
Date begin=calendarBeg.getTime();
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);
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 :)
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();
}
};
The DatePicker class has methods for getting the month, year, day of month. Or you can use an OnDateChangedListener.
Source: Stackoverflow.com