How do I set the current date in a DatePicker?

后端 未结 6 2239
夕颜
夕颜 2020-12-09 09:23

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

相关标签:
6条回答
  • 2020-12-09 10:00

    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();
    
    0 讨论(0)
  • 2020-12-09 10:05

    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);
    
    0 讨论(0)
  • 2020-12-09 10:09

    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();
    
    0 讨论(0)
  • 2020-12-09 10:10

    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.

    0 讨论(0)
  • 2020-12-09 10:13

    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
    
    0 讨论(0)
  • 2020-12-09 10:17

    Check this it has pics to demonstrate.

    0 讨论(0)
提交回复
热议问题