Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting and getting values from date picker and time picker in android

I have a DatePicker and a TimePicker in my app. Can anyone tell me how to get the values of the date and time that are selected??? What i mean to say is, for EditText we can declare as

final EditText name = (EditText) this.findViewById(R.id.nametext);

and we can cast the data of it by using name.getText().toString().

So similarly how can we get the values of DatePicker and TimePicker to a String???

like image 526
Rahul Kalidindi Avatar asked Apr 07 '10 12:04

Rahul Kalidindi


4 Answers

DatePicker has

getYear() 
getMonth() 
getDayOfMonth()

to make a Date object from a DatePicker you'd do new Date(datePicker.getYear() - 1900, datePicker.getMonth(), datePicker.getDayOfMonth());

Note that this constructor was deprecated in API level 1 - please refer to andrescanavesi's answer for a more appropriate way of creating a Date object.

TimePicker has

getCurrentHour()
getCurrentMinute()
like image 57
Jim Blackler Avatar answered Nov 04 '22 09:11

Jim Blackler


I use this:

    /**
 * 
 * @param datePicker
 * @return a java.util.Date
 */
public static java.util.Date getDateFromDatePicket(DatePicker datePicker){
    int day = datePicker.getDayOfMonth();
    int month = datePicker.getMonth();
    int year =  datePicker.getYear();

    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);

    return calendar.getTime();
}
like image 23
Andrés Canavesi Avatar answered Nov 04 '22 07:11

Andrés Canavesi


date=(EditText)findViewById(R.id.date_of_birth_textfield);

date.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 showDialog(DATE_DIALOG_ID);

            }
        });


        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

updateDisplay();

protected Dialog onCreateDialog(int id) {
        switch(id)
        {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
}
return null
}

private DatePickerDialog.OnDateSetListener mDateSetListener= new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            System.out.println("calendar view shown.."+view.getCalendarViewShown());
        c.set(Calendar.YEAR, year);
              c.set(Calendar.MONTH, monthOfYear);
              c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
              mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);
                updateDisplay();
           // mMonth = monthOfYear;
           // mDay = dayOfMonth;

        }
    };

private void updateDisplay() {
        date.setText(
                new StringBuilder()
                // Month is 0 `based` so add 1


                .append(mYear).append("-").append(mMonth + 1).append("-").append(mDay).append(""));
        //date.setText(fmtDateAndTime.format(c.getTime()));

}
like image 5
Dheeraj Giroti Avatar answered Nov 04 '22 07:11

Dheeraj Giroti


I'd suggest looking at the DateFormat class in android. Be careful to import the DateFormat mentioned in the android docs. After that, whatever formatting you want to achieve is really simple. For a basic Aug 12, 2012 you can do this.

DatePicker myDatePicker = (DatePicker) findViewById(R.id.mydatepicker);
String selectedDate = DateFormat.getDateInstance().format(myDatePicker.getCalendarView().getDate());

Look at the DateFormat class to get various other configurations.

like image 4
Sojurn Avatar answered Nov 04 '22 08:11

Sojurn