Alex Rivera | Logout

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

Asked 2010-04-07T12:48:26.340
27

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???

Edit
Report

1 Answer

3

Combined all the answers and made a code snippet out of it.

Import datePicker on the top of your class:

import android.widget.DatePicker;

Inside your class declare the following variable:

private DatePicker dobPicker;

And here is the code to be done in button click event:

 dobPicker = (DatePicker)findViewById(R.id.dobPicker);
        Integer dobYear = dobPicker.getYear();
        Integer dobMonth = dobPicker.getMonth();
        Integer dobDate = dobPicker.getDayOfMonth();
        StringBuilder sb=new StringBuilder();
        sb.append(dobYear.toString()).append("-").append(dobMonth.toString()).append("-").append(dobDate.toString()).append(" 00:00:00");
        String dobStr=sb.toString();

Where in R.id.dobPicker, dobPicker is your datePicker in the layout xml file. This gives you a string at the end, which could be stored or sent to the server.

answered 2012-08-22T08:40:29.067

Your Answer