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
setCurrentMinute) onCreate, and set that as a lower boundary.
I can’t find any functions in the Android API for doing this with the
Anyone have a suggestion to solve this problem?
Thanks in advance!
TimePicker must be in the same dialog/intent
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();
Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); // set current date into datepicker datepicker.init(year, month, day, null);
If you are simply looking for the equivalent of
TimePicker, but on
DatePicker – see updateDate (int year, int month, int dayOfMonth) on
DatePicker mDatePicker = (DatePicker) findViewById(R.id.datePicker); mDatePicker.updateDate(1994, 6, 12); // Sets date displayed on DatePicker to 12th June 1994
You can query the current date through the Calendar class.
Calendar now = Calendar.getInstance(); now.get(Calendar.WHATDOYOUNEED);
get() method with different parameters to customize your query.
The tutorial for the DatePicker seems to do exactly this?
// 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 (this method is below) updateDisplay();
Check this it has pics to demonstrate.