20

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

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
Ikky
  • 2,826
  • 14
  • 47
  • 68
  • Here is how to set the current date & set the min & the max selectable date : http://stackoverflow.com/questions/6421874/how-to-get-the-date-set-in-the-datepicker-widget-in-android/7961268#7961268 –  Nov 01 '11 at 00:42

6 Answers6

74

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();
Beryllium
  • 12,808
  • 10
  • 56
  • 86
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
20

Try this:

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);
Beryllium
  • 12,808
  • 10
  • 56
  • 86
Milen
  • 8,697
  • 7
  • 43
  • 57
11

If you are simply looking for the equivalent of setCurrentHour() and setCurrentMinute() on TimePicker, but on DatePicker - see updateDate (int year, int month, int dayOfMonth) on DatePicker.

DatePicker mDatePicker = (DatePicker) findViewById(R.id.datePicker);
mDatePicker.updateDate(1994, 6, 12);
// Sets date displayed on DatePicker to 12th June 1994
aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
Twice Circled
  • 1,410
  • 1
  • 15
  • 23
  • 1
    For some reason, DatePicker in `DatePickerDialog` does not update currently showed year if I show it, then update DatePicker, and then show it again. I had to recreate the dialog again in my case. – Vadim Kotov Jan 30 '19 at 16:08
1

You can query the current date through the Calendar class.

Calendar now = Calendar.getInstance();
now.get(Calendar.WHATDOYOUNEED);

Feed it's get() method with different parameters to customize your query.

Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
0

The tutorial for the DatePicker seems to do exactly this?

http://developer.android.com/resources/tutorials/views/hello-datepicker.html

Refer to:

    // 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();
Codemonkey
  • 3,412
  • 3
  • 20
  • 24
0

Check this it has pics to demonstrate.

Ken D
  • 5,880
  • 2
  • 36
  • 58